我正在使用isLetter()和charAt()来查看str中的字符是否是字母。 此方法的用途是查找字符串中至少包含最小字长的字数。我不想让它算数字以外的任何东西。 (im 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;
}
isLetter检查不起作用吗?它用在一个复合谓词中,它与一堆其他条件进行或运算...... –
它说“线程中的异常”主“java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4” – Seiji