3

I'm doing some assignment work and I've struck a problem.

I've been given this example code:

public class PersonNameComparator implements Comparator<Person>{
    public int compare(Person p1, Person p2) {
        int retValue = p1.getName().compareTo(p2.getName());
        if (retValue != 0)
            return retValue;
        else if (p1.getAge() < p2.getAge())
            return -1;  
        else if (p1.getAge() > p2.getAge())
            return 1;
        else
            return 0;
    }
}

However, when I try to do this, this happens:

public class DVDComparator implements Comparator <DVD> {
     
    public int compare(DVD d1,DVD d2)
        {       
         int stars1 = d1.getNoOfStars().compareTo(d2.getNoOfStars());
         //ERROR - int cannot be dereferenced.

Any ideas?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user2291903
  • 49
  • 1
  • 1
  • 7
  • Does this answer your question? [compareTo with primitives -> Integer / int](https://stackoverflow.com/questions/9150446/compareto-with-primitives-integer-int) – user202729 Feb 19 '22 at 13:18

5 Answers5

6

You get this error message since getNoOfStars() returns a primitive int and there is no method compareTo() defined for primitive types.

If you're using Java SE 7 you can use something like this:

public class DVDComparator implements Comparator <DVD> {

    public int compare(DVD d1,DVD d2){       
        return Integer.compare(d1.getNoOfStars(), d2.getNoOfStars());
    }
}
Puce
  • 37,247
  • 13
  • 80
  • 152
3

You don't need to call a method to compare primitive ints. In fact, as you've discovered, you can't.

Just use the normal <, >, and == operators to compare ints.

Just make sure to follow the contract of compare -- return an int less than 0, equal to 0, or greater than 0, if d1 is "less than" d2, if d1 is "equal to" d2, or d1 is "greater than" d2, respectively.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

rgettman
  • 176,041
  • 30
  • 275
  • 357
2

int being a primitive is not an object and thus does not have any methods. So when you try and call .compareTo on it, you get an error.

To compare ints, you can use the standard comparison operators < > ==, or you could wrap it in an Integer Object and use the compare method as you are doing now. Although the former is favorable.

Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
1

// If Person.getName() returns a string then you could simply do the following to // implement Comparable:

public class PersonNameComparator implements Comparator<Person>{
   public int compare(Person p1, Person p2) {
    return p1.getName() - p2.getName();       // It's the ninja way to do it
   }
}
  • *"If Person.getName() returns a string then you could simply do the following to ..."* create a compile time error. – Tom Dec 12 '15 at 01:51
0

You can find the reason for above error through this link. You can use one of following approaches which are simple to understand. (sorting in ascending order)

    if(d1.getNoOfStars() != d2.getNoOfStars())
    return d1.d1.getNoOfStars() - d2.getNoOfStars(); 

or

    if(d1.getNoOfStars() != d2.getNoOfStars())
    return d1.d1.getNoOfStars() > d2.getNoOfStars() ? 1 : -1; 
Aadhil Rushdy
  • 74
  • 1
  • 5