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

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

\$\endgroup\$
  • \$\begingroup\$ nice implementation, very simple. however the for loop, it would also work if you said : i <= input.length()...? \$\endgroup\$ – user116659 Dec 22 '16 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\$ – coderodde Dec 22 '16 at 18:32
  • \$\begingroup\$ what about i < input.length() or i == input.length()-1....? \$\endgroup\$ – user116659 Dec 22 '16 at 19:00
  • 4
    \$\begingroup\$ @Iona-KathrynEvans i < input.length() would be more idiomatic. \$\endgroup\$ – Malvolio Dec 22 '16 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\$ – Abion47 Dec 22 '16 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;
}
\$\endgroup\$
  • 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\$ – 4castle Dec 22 '16 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\$ – Malvolio Dec 22 '16 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\$ – Olivier Grégoire Dec 22 '16 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;
  }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy