尝试确定字符串是否仅包含Java中的字母

时间:2013-11-14 02:24:43

标签: java string loops for-loop char

我的代码如下:

} else if (words.toUpperCase().equals(words)) {
    for (int i = 0; words.length() > i; i++){
      thisLetter = words.charAt(i);
      letter = thisLetter.isLetter();
      if (!letter){
        break;
      }
    }  

letter是一个布尔值,thisLetter是一个Character类型(不是char)。 出于某种原因,我在编译时遇到以下错误:

 no suitable method found for isLetter()
method java.lang.Character.isLetter(int) is not applicable
  (actual and formal argument lists differ in length)
method java.lang.Character.isLetter(char) is not applicable
  (actual and formal argument lists differ in length)

2 个答案:

答案 0 :(得分:1)

而不是letter = thisLetter.isLetter();,它返回原始char。返回的值将由编译器

自动装入Character

Character没有方法isLetter(),相反,您应该尝试...

letter = Character.isLetter(thisLetter);

当然假设thisLetterchar ...

有关详细信息,请参阅Java Docs

答案 1 :(得分:0)

承租人:

boolean containsOnly = true;
for (int i = 0; i < words.length(); i++) {
    char theChar = words.charAt(i);
    int index = "Java".indexOf(theChar);
    if (index < 0) {
       containsOnly = false;
       break;
    }
}