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

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

ResortOrganizer.java:159: error: cannot find symbol         else if (resortList[mid].compareTo(resortName)>0)                                  ^ 

symbol: method compareTo(String) location: class Resort

2 errors

Can someone explain why it does this? I have a feeling I'm forgetting something important.

This question is related to java string class methods main

The answer is


It seem like your Resort method doesn't declare a compareTo method. This method typically belongs to the Comparable interface. 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. As such, you shouldn't be passing a String argument, but rather a Resort.

Alternatively, you can compare the names of the resorts. For example

if (resortList[mid].getResortName().compareTo(resortName)>0)  

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to class

String method cannot be found in a main class method Class constructor type in typescript? ReactJS - Call One Component Method From Another Component How do I declare a model class in my Angular 2 component using TypeScript? When to use Interface and Model in TypeScript / Angular Swift Error: Editor placeholder in source file Declaring static constants in ES6 classes? Creating a static class with no instances In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric Static vs class functions/variables in Swift classes?

Examples related to methods

String method cannot be found in a main class method Calling another method java GUI ReactJS - Call One Component Method From Another Component multiple conditions for JavaScript .includes() method java, get set methods includes() not working in all browsers Python safe method to get value of nested dictionary Calling one method from another within same class in Python TypeError: method() takes 1 positional argument but 2 were given Android ListView with onClick items

Examples related to main

String method cannot be found in a main class method How to access global variables Maven Error: Could not find or load main class Eclipse error "Could not find or load main class" Error: Main method not found in class Calculate, please define the main method as: public static void main(String[] args) What does "Could not find or load main class" mean? C# importing class into another class doesn't work In Python, can I call the main() of an imported module? Can there exist two main methods in a Java program? Could not find or load main class with a Jar File