5

I'm currently getting an error when attempting to sort these Pet objects by weight. I'm sure it's something simple, but I can't see why this compare isn't working.

Error: The return type is incompatible with java.util.Comparator.compare(Pet, Pet)

ArrayListNoDups class

import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.lang.Object;

public class ArrayListNoDups {
    public static void main(String[] args) {
        ArrayList<Pet> list = new ArrayList<Pet>();
        String name;
        Integer age;
        Double weight;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("If you wish to stop adding Pets to the list, please put a 0 for all 3 fields.");

        do {
            System.out.println("Enter a String for Pet name: ");
            name = keyboard.next();
            System.out.println("Enter an int for Pet age: ");
            age = keyboard.nextInt();
            System.out.println("Enter a double for Pet weight: ");
            weight = keyboard.nextDouble();

            if (name.length() > 0 && age > 0 && weight > 0)
                list.add(new Pet(name, age, weight));
        } while (name.length() > 0 && age > 0 && weight > 0);

        System.out.println("Your list sorted by WEIGHT ========================= ");
        Collections.sort(list, Pet.SortByWeight);
        for (Pet p2 : list)
            p2.writeOutput();

    }
}

Pet class

import java.util.*;

public class Pet {
    private String name;
    private Integer age; // in years
    private double weight; // in pounds

    public void writeOutput() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age + " years");
        System.out.println("Weight: " + weight + " pounds");
    }

    public void set(String newName) {
        name = newName;
        // age and weight are unchanged.
    }

    public void set(int newAge) {
        if (newAge <= 0) {
            System.out.println("Error: illegal age.");
            System.exit(0);
        } else
            age = newAge;
        // name and weight are unchanged.
    }

    public void set(double newWeight) {
        if (newWeight <= 0) {
            System.out.println("Error: illegal weight.");
            System.exit(0);
        } else
            weight = newWeight;
        // name and age are unchanged.
    }

    public Pet(String name, int age, double weight) {
        this.name = name;
        this.age = age;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getWeight() {
        return weight;
    }

    public static Comparator<Pet> SortByWeight = new Comparator<Pet>() {
        public double compare(Pet pet1, Pet pet2) {
            return pet1.getWeight() - pet2.getWeight();
        }
    };
}

3 Answers 3

7

compare method of Comparator interface returns an int, not double.

Change:

public double compare(Pet pet1, Pet pet2)

to:

public int compare(Pet pet1, Pet pet2)

The Comparator can look like this:

public static Comparator<Pet> SortByWeight = new Comparator<Pet>() {
    public int compare(Pet pet1, Pet pet2) {
        return (int)(pet1.getWeight() - pet2.getWeight());
    }
};
Sign up to request clarification or add additional context in comments.

3 Comments

@MartDellon Just cast the difference to int - return (int)(pet1.getWeight() - pet2.getWeight());
For visibility, this works just as well as Ankit's answer. I just tried his first. Thank you both for the help!
@MartDellon Note that if you use Ankit's solution, each comparison will involve creation of two Double objects that would immediately be eligible for garbage collection. A decent sorting algorithm of n elements requires nlog(n) comparisons, which means you'll create 2*nlog(n) Double objcets and give extra work to the garbage collector. This will probably make the sorting slower than my solution, which works on primitive types and doesn't create any objects.
0

The problem is compare needs to return an integer to comply with that Comparator interface.

You could cast down to an int in your comparison, or perhaps better, just use Double.compare().

Side note: using the @Override annotation in these situations (implementing common interfaces) will make the compiler help you see these problems earlier.

Comments

0

Try this :

public int compare(Pet pet1, Pet pet2) {
            return new Double(pet1.getWeight()).compareTo(new Double(pet2.getWeight()));
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.