错误:尝试使用compareTo将一个字符串与另一个字符串进行比较时,类型不兼容
这是我的代码,接受 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]);
}
}
}
This is my code to accept 50 names and roll no. and print them alphabetically . It is giving error incompatible type for 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]);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
compareTo
返回一个int
而不是boolean
。你想要的是:
编辑 另外,你应该检查 null:
compareTo
returns anint
not aboolean
.What you want is:
EDIT Also, you should check for null:
CompateTo 返回一个整数,而不是布尔值。你的代码应该是这样的
if(name[j].compareTo(small) > 1)
...
CompateTo returns an integer, not a boolean. Your code should be something like
if(name[j].compareTo(small) >1)
...
您仍然可以使用
compareTo
,但整个表达式需要返回boolean
。if
语句需要true
或false
来做出决定(而不是数字)。这相当于.equals。您还可以使用 >例如,使用 0 来查看
name[j]
是否大于compareTo
定义的small
。You can still use
compareTo
but the entire expression needs to return aboolean
. Anif
statement requires atrue
orfalse
to make its decision (not a number).This is equivalent to .equals. You can also use > 0 for instance to see if
name[j]
is greater thansmall
as defined bycompareTo
.有一个明显的错误,比如,当外层for循环开始时,外层i为零,name[0]是你的输入,例如'zzzzz',mark[0]是3443,例如。所以内部循环开始,它从零开始 t0 49 ,并在 name[0-49] 和小 、 that 、 pre name[i] 之间进行比较...因为名称数组中只有一个元素.. 。所以也许只是在 null 和其他一些字符串之间进行比较......错误也很明显......我认为这是主要错误,你可以自己检查......你...... ...
there are an obvious error , for instance , when the outer for loop begin , the outer i is zero, name[0] is your input , for instance 'zzzzz', the mark[0] is 3443, for example . so the inner loop begin , it begin from zero t0 49 , and make a comopare between name[0-49] and the small , that , the pre name[i]... because there are only one element in name array ... so the maybe just make a compare beteen null and other some String ... the error alose so obvious..... i think this is the main error , you can check it for yourself..... bery you .....