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).
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\$