public class Alphabet {
public static void main(String[] args) {
checkAlphabetic("fdsfsfds+");
}
public static boolean checkAlphabetic(String input) {
char[] chars = input.toCharArray();
int count = 0;
for (int i = 0; i < input.length(); i++) {
if (Character.isLetter(chars[i])) {
count = 1;
} else {
count = 0;
break;
}
}
if (count >= 1) {
System.out.println("alphabetic word");
return true;
} else {
System.out.println("word is not alphabetic");
return false;
}
}
}
I know people will say to use regex as it's more efficient but our tutor wanted us to use loops, as we haven't learned about regex yet (old school course).
3 Answers 3
It's not harder than this:
public static boolean checkAlphabetic(String input) {
for (int i = 0; i != input.length(); ++i) {
if (!Character.isLetter(input.charAt(i))) {
return false;
}
}
return true;
}
The idea is to return false
as soon as you encounter a character c
for which Character.isLetter
returns false
. If no such, return true
since the string does not contain non-letter characters.
-
\$\begingroup\$ nice implementation, very simple. however the for loop, it would also work if you said : i <= input.length()...? \$\endgroup\$user116659– user1166592016年12月22日 18:05:04 +00:00Commented Dec 22, 2016 at 18:05
-
\$\begingroup\$ @Iona-KathrynEvans No, for the last iteration
i
will beinput.length()
which is one past the last accessible index. \$\endgroup\$coderodde– coderodde2016年12月22日 18:32:33 +00:00Commented Dec 22, 2016 at 18:32 -
\$\begingroup\$ what about i < input.length() or i == input.length()-1....? \$\endgroup\$user116659– user1166592016年12月22日 19:00:15 +00:00Commented Dec 22, 2016 at 19:00
-
4\$\begingroup\$ @Iona-KathrynEvans
i < input.length()
would be more idiomatic. \$\endgroup\$Michael Lorton– Michael Lorton2016年12月22日 19:29:01 +00:00Commented Dec 22, 2016 at 19:29 -
1\$\begingroup\$ @Darkhogg The for loop will continue to loop for as long as the condition is true. Since the starting value for
i
is 0,i == input.length() - 1
will only be true ifinput.length() - 1 == 0
, orinput.length() == 1
. Otherwise, the condition will return false and the program won't even enter the loop at all. \$\endgroup\$Abion47– Abion472016年12月22日 23:04:09 +00:00Commented Dec 22, 2016 at 23:04
Alternatively, you can use an enhanced for loop if you are using Java 5 or superior.
public boolean checkAlphabetic(String input) {
if (input == null) return false;
for (char c : input.toCharArray()) {
if (!Character.isLetter(c)) {
return false;
}
}
return true;
}
-
1\$\begingroup\$ Using an enhanced for loop will be slightly less performant in this case, because
toCharArray
must make a copy of the internal character array. \$\endgroup\$castletheperson– castletheperson2016年12月22日 16:01:13 +00:00Commented Dec 22, 2016 at 16:01 -
6\$\begingroup\$ @4castle -- "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. " -- Donald Knuth \$\endgroup\$Michael Lorton– Michael Lorton2016年12月22日 19:30:19 +00:00Commented Dec 22, 2016 at 19:30
-
\$\begingroup\$ I'd only remove the null-check: I personally prefer to get
NullPointerException
so I know I passed anull
value, and then I try to avoid calling the method at all. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2016年12月22日 21:41:57 +00:00Commented Dec 22, 2016 at 21:41
Here is my optimisation further:
public class Alphabet {
public static void main(String[] args) {
checkAlphabetic("fdsfsfd432423423!");
}
public static boolean checkAlphabetic(String input) {
char[] chars = input.toCharArray();
int count = 0;
for (int i = 0; i < input.length(); i++) {
if (Character.isLetter(chars[i])) {
count = 1;
} else {
System.out.println("String is not alphabetic....");
System.exit(1);
return false;
}
}
System.out.println("alphabetic word");
return true;
}
}
return input.chars().allMatch(Character::isLetter);
\$\endgroup\$::
operator is used to make a method reference. The syntax is new in Java 8. It's purpose is essentially to pass a method as a parameter. Your course instructor will likely not accept this as a solution, because it is often treated as an advanced topic. \$\endgroup\$