简体   繁体   English

Java确定字符串中的每个字符是否为字母

[英]Java determining if each character in a string is a letter

I'm using isLetter() and charAt() to see if a character in str is a letter or not. 我正在使用isLetter()和charAt()来查看str中的字符是否是字母。 The use of this method is to find the number of words in a string that are at least the minimum word length. 此方法的用途是查找字符串中至少等于最小单词长度的单词数。 I don't want it to count anything other than letters. 除了字母,我不希望它计数任何其他东西。 (im a java noob) (我是Java菜鸟)

public static int wordCount(String str, int minLength) {     
//Method that counts words.

    int count = 0;
    boolean isLetter = false;
    for (int i = 0, wordLength = 0; i < str.length() + 1; i++) {
        isLetter = Character.isLetter(str.charAt(i)); //Why doesn't this line work?
        if (i == str.length() || str.charAt(i) == ' ' || isLetter==false) { 
            if (wordLength >= minLength) {              
                count++;                                                
            }
            wordLength = 0;                                 
        } else {                                                            
            wordLength++;                                       
        }
    }
    return count;
}

The problem is in your for loop. 问题出在您的for循环中。 You have it iterate until i < str.length() + 1 . 您需要迭代直到i < str.length() + 1为止。 That means at some point, you will be asking for str.charAct(str.length()) . 这意味着在某个时候,您会要求提供str.charAct(str.length()) Because Java arrays (and Strings) index from 0, the index is too high and will break. 因为Java数组(和字符串)的索引从0开始,所以索引太高并且会中断。

Instead, just have your for loop iterate until i < str.length() . 相反,只需让for循环迭代直到i < str.length() That should fix it. 那应该解决它。

It's not the Character.isLetter not working, it's the str.charAt(i) that throws an exception when i reaches str.length() . 不是Character.isLetter无法正常工作,而是当i达到str.length()时, str.charAt(i)引发异常。

Your loop iterates i from 0 to str.length() , inclusive - a total of str.length()+1 characters. 您的循环将i0迭代到str.length() (包括str.length() -总共str.length()+1字符。 The call of charAt(i) when i is str.length() would be illegal. istr.length()时,调用charAt(i) str.length()将是非法的。

To fix this problem, remove + 1 from the expression in the for loop, or make sure that str.charAt(i) is not called when i is equal to str.length() . 要解决此问题,请从for循环的表达式中删除+ 1 ,或确保当i等于str.length()时不调用str.charAt(i) str.length() For example, you could use the short-circuiting feature like this: 例如,您可以使用以下短路功能:

for (int i = 0, wordLength = 0; i < str.length() + 1; i++) {
    if (i == str.length() || !Character.isLetter(str.charAt(i))) {
        ...
    }
}

Above, str.charAt(i) is not called when i == str.length() , preventing the crash ( demo ). 上面,当i == str.length()时不调用str.charAt(i) ,以防止崩溃( demo )。

You may want to change a few more things: 您可能需要更改一些其他内容:

  • str.charAt(i) == ' ' || isLetter==false str.charAt(i) == ' ' || isLetter==false can be reduced to isLetter==false or even to !isLetter , because space character is not a letter str.charAt(i) == ' ' || isLetter==false可以简化为isLetter==false甚至可以简化为!isLetter ,因为空格字符不是字母
  • The declaration of isLetter can be moved inside the loop, and combined with initialization (unless you decide to get rid of the variable altogether using the code snipped shown above). 可以在循环内移动isLetter的声明,并将其与初始化结合使用(除非您决定使用上面显示的代码完全isLetter该变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM