[英]String method cannot be found in a main class method
I have about a one year experience programming Java, I made a static method located in the main class, and for some reason, whenever I compile it gives me an error saying that the compareTo() method from the String class can't be found. 我有大约一年的Java编程经验,我在主类中创建了一个静态方法,由于某种原因,每当我编译它时,都会出现一个错误,提示找不到String类的compareTo()方法。 。
Here's the method: 方法如下:
public static void displaySpecificResort(String resortName, Resort[] resortList)
{
int low = 0;
int high = resortList.length - 1;
int mid;
while (low <= high)
{
mid = low + (high - low);
if (resortList[mid].compareTo(resortName)<0)
low = mid + 1;
else if (resortList[mid].compareTo(resortName)>0)
high = mid - 1;
else resortList[mid].display();
}
if(resortList[mid].getName().compareTo(resortName)!= 0)
System.out.println("Resort could not be found.");
}
Here's the error it gives me when I try and compile: 这是我尝试编译时给我的错误:
ResortOrganizer.java:157: error: cannot find symbol
if (resortList[mid].compareTo(resortName)<0)
^
symbol: method compareTo(String) location: class Resort 符号:方法compareTo(String)位置:类Resort
ResortOrganizer.java:159: error: cannot find symbol
else if (resortList[mid].compareTo(resortName)>0)
^
symbol: method compareTo(String) location: class Resort 符号:方法compareTo(String)位置:类Resort
2 errors 2个错误
Can someone explain why it does this? 有人可以解释为什么这样做吗? I have a feeling I'm forgetting something important. 我有种忘记重要的感觉的感觉。
It seem like your Resort
method doesn't declare a compareTo
method. 看来您的Resort
方法没有声明compareTo
方法。 This method typically belongs to the Comparable
interface. 此方法通常属于Comparable
接口。 Make sure your class implements it. 确保您的课程实现了它。
Additionally, the compareTo
method is typically implemented as accepting an argument of the same type as the object the method gets invoked on. 此外, compareTo
方法通常实现为接受与调用该方法的对象相同类型的参数。 As such, you shouldn't be passing a String
argument, but rather a Resort
. 因此,您不应传递String
参数,而应传递Resort
。
Alternatively, you can compare the names of the resorts. 另外,您可以比较度假村的名称。 For example 例如
if (resortList[mid].getResortName().compareTo(resortName)>0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.