错误:尝试使用compareTo将一个字符串与另一个字符串进行比较时出现不兼容的类型

1 java bluej

这是我的代码接受50个名字和滚动号.并按字母顺序打印.它为if(name [j] .compareTo(small))提供错误不兼容的类型

import java .io.*;
class student
{
    public void main()throws IOException
    {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      String name[]=new String[50];
      int mark[]=new int[50];
      int i;
      for( i=0;i<=49;i++)
      {
        System.out.println("plz ntr d name of d studnt");
        name[i]=br.readLine();
        System.out.println("plz ntr d marks of d studnt");
        mark[i]=Integer.parseInt(br.readLine());
        int j,pos=0;
        String temp, small;
        for(i=0;i<49;i++)
        {
          small=name[i];
          pos=i;
          for(j=i+1;j<49;j++)
          {
            if(name[j].compareTo(small))
              pos=j;
          }
        }
        temp=name[i];
        name[i]=name[pos];
        name[pos]=temp;
      }
      for(i=0;i<=49;i++)
      {
        System.out.println((i+1)+" "+name[i]+" "+mark[i]);
      }
    }
 }
Run Code Online (Sandbox Code Playgroud)

Gui*_*ume 6

compareTo返回int不是boolean.

你想要的是:

if(name[j].equals(small)) {
Run Code Online (Sandbox Code Playgroud)

编辑另外,你应该检查null:

if (name[j] != null && name[j].equals(small)) {
Run Code Online (Sandbox Code Playgroud)