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

Malachi
29k11 gold badges86 silver badges188 bronze badges
asked Dec 22, 2016 at 14:18
\$\endgroup\$
4
  • 4
    \$\begingroup\$ In Java 8: return input.chars().allMatch(Character::isLetter); \$\endgroup\$ Commented Dec 22, 2016 at 16:08
  • \$\begingroup\$ The syntax of that is strange. For example havent seen :: before...looks like Ruby \$\endgroup\$ Commented Dec 22, 2016 at 18:02
  • \$\begingroup\$ The :: 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\$ Commented Dec 22, 2016 at 18:17
  • \$\begingroup\$ In the point of program structure, you shouldn't print anything inside the check function, just return a value. \$\endgroup\$ Commented Dec 23, 2016 at 1:02

3 Answers 3

6
\$\begingroup\$

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.

answered Dec 22, 2016 at 15:10
\$\endgroup\$
8
  • \$\begingroup\$ nice implementation, very simple. however the for loop, it would also work if you said : i <= input.length()...? \$\endgroup\$ Commented Dec 22, 2016 at 18:05
  • \$\begingroup\$ @Iona-KathrynEvans No, for the last iteration i will be input.length() which is one past the last accessible index. \$\endgroup\$ Commented Dec 22, 2016 at 18:32
  • \$\begingroup\$ what about i < input.length() or i == input.length()-1....? \$\endgroup\$ Commented Dec 22, 2016 at 19:00
  • 4
    \$\begingroup\$ @Iona-KathrynEvans i < input.length() would be more idiomatic. \$\endgroup\$ Commented 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 if input.length() - 1 == 0, or input.length() == 1. Otherwise, the condition will return false and the program won't even enter the loop at all. \$\endgroup\$ Commented Dec 22, 2016 at 23:04
6
\$\begingroup\$

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;
}
answered Dec 22, 2016 at 15:31
\$\endgroup\$
3
  • 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\$ Commented 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\$ Commented 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 a null value, and then I try to avoid calling the method at all. \$\endgroup\$ Commented Dec 22, 2016 at 21:41
0
\$\begingroup\$

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;
 }
}
answered Jan 4, 2017 at 15:14
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.