I'm implementing a sorted list class , in this class i'm going to sort any kind of objects , so now i want to test if the object is comparable or not ,
i used this code to override the compareTo() method :-
class MyObject implements Comparable {
Object ob;
MyObject(){
this.ob=new Object();
}
public int compareTo(Object current){
if((ob.toString().compareTo(current.toString()))>1)
return 1;
else if((ob.toString().compareTo(current.toString()))<1)
return -1;
else
return 0;
}
public String toString(){
return (String)ob;
}
}
so now i need to assign numbers to these objects like this
class SortedListTest{
public static void main (String arg[]){
MyObject b1= new MyObject();
MyObject b2= new MyObject();
b1.ob=6;
b2.ob=3;
System.out.println(b2.compareTo(b1));
}
}
but it keeps giving me this exception:-
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String