简体   繁体   中英

'Can't find symbol' error in compare method for a Sorting program

I have an assignment for class in which we must sort an ArrayList< Object> containing Integer, Double, Book and PlayingCard objects. We HAVE to use the 'instanceof' keyword and 'compareTo' method for the sorting. I'm having trouble with my method named "compare" which takes two elements in the list and checks what type they are and then if they are the same type, uses the compareTo method to sort them. Integers and Doubles should be treated as the same type as they are sorted together, which I know would give problems with the compareTo method, as it can only compare two objects of same type. I keep getting a 'Can't find symbol' error whenever I try to call the compareTo method using the two elements that are passed through (a.compareTo(b)). I've tried converting them from Object type to their respective class types...to no avail. I'm just not sure what I'm doing wrong here. Any help is greatly appreciated. If you need to see the Book or PlayingCard classes' code, I can supply that too. (And yes, both Book and PlayingCard have their own compareTo methods and both implement Comparable).

Here's the code:

import java.util.ArrayList;

public class Sorter {
    /**
     *  Checks two objects in the list to see what type they are,
     *  if they are the same type, use compareTo method to sort,
     *  otherwise, sort in order of Integer and Double > Book > PlayingCard.
     *
     * @param a  first object
     * @param b  second object
     * @return  a negative number if a < b, a positive number if a > b,
     *          0 if a = b
     */
    public static int compare(Object a, Object b) {
        if ((a instanceof Integer || a instanceof Double) && (b instanceof Integer || b instanceof Double)) {
            return a.compareTo(b);
        }
        else if ((a instanceof Integer || a instanceof Double) && (b instanceof Integer == false || b instanceof Double ==  false)) {
            return -1;
        }
        else if ((a instanceof Integer == false || a instanceof Double == false) && (b instanceof Integer || b instanceof Double)) {
            return 1;
        }
        else if ((a instanceof Book) && (b instanceof PlayingCard)) {
            return -1;
        }
        else if ((a instanceof PlayingCard) && (b instanceof Book)) {
            return 1;
        }
        else if ((a instanceof Book) && (b instanceof Book)) {
         return a.compareTo(b);
        }
        else {
         return a.compareTo(b);
        }
    }

/**
 * Sort a list of objects. Uses the selection sort algorithm.
 *
 * @param stuff  list of objects
 */
public static void sort(ArrayList<Object> stuff) {
    // selection sort
    for (int i = 0; i < stuff.size() - 1; i++) {
        int lowest = i;
        for (int j = 1; j < stuff.size(); j++) {
            if (compare(stuff.get(j), stuff.get(lowest)) < 0) {
                lowest = j;
            }
        }

        // swap to front
        if (lowest != i) {
            Object temp = stuff.get(i);
            stuff.set(i, stuff.get(lowest));
            stuff.set(lowest, temp);
        }
    }
}

/**
 * Main method. Populates an arraylist of stuff and sorts it.
 *
 * @param args  command-line arguments
 */
public static void main(String[] args) {
     ArrayList<Object> list = new ArrayList<>();

     list.add(8);
     list.add(new PlayingCard(PlayingCard.HEARTS, PlayingCard.TWO));
     list.add(3.5);
     list.add(new Book("Mark Twain", "The Adventures of Huckleberry Finn"));
     list.add(new Book("F. Scott Fitzgerald", "The Great Gatsby"));
     list.add(5.65);
     list.add(new PlayingCard(PlayingCard.CLUBS, PlayingCard.SEVEN));
     list.add(new PlayingCard(PlayingCard.SPADES, PlayingCard.ACE));

     System.out.println("Original List: \n" + list); //debugging help
     sort(list);
     System.out.println("Sorted List: \n" + list);
}

}

And here is the compiler error:

  Sorter.java:16: error: cannot find symbol
                    return a.compareTo(b);
                            ^
  symbol:   method compareTo(Object)
  location: variable a of type Object
  Sorter.java:31: error: cannot find symbol
                     return a.compareTo(b);
                             ^
  symbol:   method compareTo(Object)
  location: variable a of type Object
  Sorter.java:34: error: cannot find symbol
                     return a.compareTo(b);
                             ^
  symbol:   method compareTo(Object)
  location: variable a of type Object
  3 errors

PlayingCard's compareTo method:

public int compareTo(PlayingCard other) {
      if (getSuit() < other.getSuit()) {
          return -1;
      }
      else if (getSuit() > other.getSuit()) {
          return 1;
      }
      else {
          if (getRank() < other.getRank()) {
              return -1;
          }
          else if (getRank() > other.getRank()) {
              return 1;
          }
          else {
              return 0;
          }
      }
  }

Book's compareTo method:

public int compareTo(Book other) {
      if (getAuthor().compareTo(other.getAuthor()) < 0) {
          return -1;
      }
      else if (getAuthor().compareTo(other.getAuthor()) > 0) {
          return 1;
      }
      else {
          if (getTitle().compareTo(other.getTitle()) < 0) {
              return -1;
          }
          else if (getTitle().compareTo(other.getTitle()) > 0) {
              return 1;
          }
          else {
              return 0;
          }
      }
  }

You are calling compareTo on an instance of class Object. If you look up class Object ( https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html ), you will see it has no such method. That is what the compiler is telling you, although it is using the word "symbol" to represent either method or field.

Before calling compareTo you need to cast your object to an instance of either Integer or Double, like the following code, and then you can use the compareTo method of Integer, or the compareTo method of Double, depending on what you have:

if(a instanceof Double && b instanceof Double) {
     Double aDouble = (Double) a;
     Double bDouble = (Double) b;
     return aDouble.compareTo(b);
}

Most likely in this case the exercise wants you to compare Integer with Double. For that you need to choose which compareTo method you can use, and if you look into conversions and the representations of int/double, then the one you should choose is from Integer to Double, so you will need something like this as one of the options:

if(a instanceof Integer && b instanceof Double) {
     Integer aInteger = (Integer) a;
      Double bDouble = (Double) b;
     return -bDouble.compareTo(aInteger.doubleValue()); //note the negative
}

and this as another option:

if(a instanceof Double && b instanceof Integer ) {
     Double aDouble = (Double) a;
      Integer  bInteger = (Integer ) b;
     return aDouble.compareTo(bInteger.doubleValue());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM