Char不能被解除引用?使用compareTo

jjh*_*son 1 java compareto

我正在尝试创建一个将读取字符串的程序,并比较字符串中的每个字符,看它是否按字母顺序排列.

public class Main
{
    public static void Main ( String[] args)
    {
        System.out.println("#Please enter the string: ");
        String s = BIO.getString();

        while(!s.equals("END")){
            int length = s.length();
            String sLC = s.toLowerCase();
            int count = 0;
            boolean inOrder = true;

            for(int i = 0; i < length - 1 ; i++){
                if(sLC.charAt(i).compareTo(sLC.charAt(i+1)) > 0) {
                    inOrder = false;
                    break;
                }   
            }  

            System.out.println("#Please enter the string: ");  
            s = BIO.getString();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用blueJ,当我尝试编译它时,它给出了错误'char无法解除引用并在我的IF语句中突出显示'compareTo'方法?

Eri*_*ric 6

.charAt()返回a char,这是一个原语.它没有.compareTo()方法.

char表现得很像(较小)int; 请改用以下内容:

if(sLC.charAt(i) > sLC.charAt(i+1)) {
Run Code Online (Sandbox Code Playgroud)