1

When I am trying to execute the below code,the compiler is throwing error at line 13 as "java.lang.ClassCastException". Can someone let me know what's wrong with below code?

package chapter11;

import java.util.*;

public class ComparableExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Item[] items = new Item[3];
        items[0] = new Item(102, "Duct Tape");
        items[1] = new Item(103, "Bailing Wire");
        items[2] = new Item(104, "Chewing Gum");

        Arrays.sort(items);
        for (Item i : items) {
            System.out.println(i.getNumber() + ":" + i.getDescription());
        }
    }
}

interface Comparable {
    int compareTo(Object o);
}

class Item implements Comparable {
    private int number;
    private String description;

    public Item(int number, String description) {
        this.number = number;
        this.description = description;
    }

    public int getNumber() {
        return number;
    }

    public String getDescription() {
        return description;
    }

    public int compareTo(Object o) {
        Item i = (Item) o;
        if (this.getNumber() < i.getNumber())
            return -1;
        if (this.getNumber() < i.getNumber())
            return 1;
        return 0;
    }
}

Any help is appreciated, Thanks!!

5
  • 7
    Why have you created your own Comparable interface? Commented Mar 3, 2014 at 18:46
  • Your compareTo method in the Item class can be simplified to: return this.number - i.number; compareTo does not dictate that you return -1,1,or 0. You only need to return a pos/neg number or 0. Commented Mar 3, 2014 at 18:50
  • 1
    You're checking for < i.getNumber() twice. 1 will never be returned. Commented Mar 3, 2014 at 18:51
  • @Solace Frankly, no. That is not a good simplification. There is a chance of overflow when the result goes out of bounds. Best is to use Integer.compare() method. Commented Mar 3, 2014 at 18:57
  • @RohitJain That's a fair point. Commented Mar 3, 2014 at 19:07

1 Answer 1

2

Remove your Comparable interface, and use the Comparable interface from the Java api. And also, maybe you can change

public int compareTo(Object o) {
        Item i = (Item) o;
        if (this.getNumber() < i.getNumber())
            return -1;
        if (this.getNumber() < i.getNumber())
            return 1;
        return 0;
    }

into :

public int compareTo(Object o) {
            Item i = (Item) o;
            if (this.getNumber() < i.getNumber())
                return -1;
            if (this.getNumber() > i.getNumber())
                return 1;
            return 0;
        }
Sign up to request clarification or add additional context in comments.

4 Comments

In this case, fix the raw type.
Comparable is an interface. Both in OP code, and in standard Java API.
Removing Comparable interface doesn't change a thing. It still gives the same error(Exception in thread "main" java.lang.ClassCastException: chapter11.Item cannot be cast to java.lang.Comparable) at Line 14..Also,changed the compareTo class
@RohitJain That's true, I wrote the answer without re-reading it

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.