尝试确定字符串在Java中是否只包含字母
我的代码如下:尝试确定字符串在Java中是否只包含字母,java,string,loops,for-loop,char,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; } } 字母是布尔值,thisLetter是字
} else if (words.toUpperCase().equals(words)) {
for (int i = 0; words.length() > i; i++){
thisLetter = words.charAt(i);
letter = thisLetter.isLetter();
if (!letter){
break;
}
}
字母是布尔值,thisLetter是字符类型(不是字符)。
由于某些原因,我在编译时出现以下错误:
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)
而不是
letter=thisLetter.isLetter()
,它返回一个原语char
。编译器将返回的值自动装箱到字符中
Character
没有方法isleter()
,您应该尝试
letter = Character.isLetter(thisLetter);
当然,假设这个字母
是字符
有关更多详细信息,请咨询承租人:
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;
}
}
boolean containsOnly=true;
for(int i=0;i
words.toUpperCase().equals(words)是什么意思?它必须是大写字母“J”吗?这段代码的重点是确定字符串是否都是大写字母。这段代码检查这一点,但如果单词中有非字母,它也会返回true。thisLetter.isLetter()
是错误的。应该是Character.isLetter(这封信)
。这很有效,谢谢!但是我所做的有什么问题?Character
没有方法isleter()
,它有一个名为isleter(char)