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

share|improve this question
4  
In Java 8: return input.chars().allMatch(Character::isLetter); – 4castle 13 hours ago
    
The syntax of that is strange. For example havent seen :: before...looks like Ruby – Iona-Kathryn Evans 11 hours ago
    
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. – 4castle 11 hours ago
    
In the point of program structure, you shouldn't print anything inside the check function, just return a value. – Pavel 4 hours ago
up vote 7 down vote accepted

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.

share|improve this answer
    
nice implementation, very simple. however the for loop, it would also work if you said : i <= input.length()...? – Iona-Kathryn Evans 11 hours ago
    
@Iona-KathrynEvans No, for the last iteration i will be input.length() which is one past the last accessible index. – coderodde 11 hours ago
    
what about i < input.length() or i == input.length()-1....? – Iona-Kathryn Evans 10 hours ago
3  
@Iona-KathrynEvans i < input.length() would be more idiomatic. – Malvolio 10 hours ago
1  
@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. – Abion47 6 hours ago

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;
}
share|improve this answer
1  
Using an enhanced for loop will be slightly less performant in this case, because toCharArray must make a copy of the internal character array. – 4castle 13 hours ago
6  
@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 – Malvolio 10 hours ago
    
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. – Olivier Grégoire 7 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.