简体   繁体   English

在主类方法中找不到字符串方法

[英]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.

相关问题 Java- Main方法在类中找不到 - Java- Main method cannot be found in class 在课堂上找不到主要方法 - Main method not found in class 错误:在类mainGUI中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class mainGUI, please define the main method as: public static void main(String[] args) “错误:在Grad类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)” - “Error: Main method not found in class Grad, please define the main method as: public static void main(String[] args)” 错误:在 class 中找不到主要方法,请将主要方法定义为:public static void main(String[] args) - Error: Main method not found in class, please define the main method as: public static void main(String[] args) 错误:在类Text中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Text, please define the main method as: public static void main(String[] args) 错误:在Binary类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Binary, please define the main method as: public static void main(String[] args) 错误:在TextBook类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class TextBook, please define the main method as: public static void main(String[] args) 在 class _____ 中找不到主要方法。 请将主要方法定义为:public static void main(String[] args) - Main Method not found in class _____. Please define the main method as: public static void main(String[] args) 在ActivityTime类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Main method not found in class ActivityTime, please define the main method as: public static void main(String[] args)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM