Softey
Softey

Reputation: 1491

Java CompareTO method issues

I am trying to sort out a simple list of students mark with a simple java program however I am getting

Exception in thread "main" java.lang.ClassCastException: Student cannot be cast to java.lang.Comparable

public class Student {
public String name;
public int mark;


public Student(String name, int mark){
    this.name=name;
    this.mark=mark;
}


public int compareTo(Student o){
    return this.mark-o.mark;


}
public String toString(){
    String s = "Name: "+name+"\nMark: "+mark+"\n";
    return s;
}

public static void main(String[] args) {
    Student Class[] = new Student[9];
    Class[0] = new Student("Henry",100);
    Class[1] = new Student("Alex", 10);
    Class[2] = new Student("Danielle",100);
    Class[3] = new Student("Luke",10);
    Class[4] = new Student("Bob",59);
    Class[5] = new Student("Greg",76);
    Class[6] = new Student("Cass",43);
    Class[7] = new Student("Leg",12);
    Class[8] = new Student("Bobe",13);



    Arrays.sort(Class);
    for(int i = 0;i<Class.length;i++){
        System.out.println(Class[i]);

Upvotes: 0

Views: 185

Answers (3)

fz557
fz557

Reputation: 1

public class Fawaz1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // تجربه 
        String SS[]=new String[6];
        double GG[]=new double[6];
        SS[0]="fawaz";  
        SS[1]="ahmd";
        SS[2]="hmd";
        SS[3]="fos";
        SS[4]="raid";
        SS[5]="majd";
        
        GG[0]=3.94;
        GG[1]=2.50;
        GG[2]=2.95;
        GG[3]=4.92;
        GG[4]=3.0;
        GG[5]=3.78;
        
        int i;
        for (i=0; i<3; i++){
            System.out.print(SS[i]+"\t"+GG[i]+"\t");
            if (GG[i]>=4.75)
            {System.out.println("A+");}
            else if(GG[i]>=4.50){
                System.out.println("A");
            } else if(GG[i]>=3.70){
                System.out.println("B+");
            }else if(GG[i]>=3.59){
                System.out.println("B");
            }else if(GG[i]>=2.78){
                System.out.println("C+");
            }else if(GG[i]>=2.55){
                System.out.println("C");
            }else if(GG[i]>=1.52){
                System.out.println("D");
            }else if(GG[i]>=1.10){
                System.out.println("F");
            }
        }
        
        
        
        
    }
    
}

Upvotes: -1

Rohit Jain
Rohit Jain

Reputation: 213223

Make your Student class implement Comparable<Student>. The compareTo() method doesn't work on it's own while sorting.

Also, Class doesn't look like a very good variable name. How about using students? Also, I see an issue in your compareTo method:

public int compareTo(Student o){
    return this.mark-o.mark;
}

Never compare on the result of subtraction of 2 integers, or longs. The result might overflow. Rather use Integer.compare(int, int) method.
Also, get rid of public fields. Make them private, and provide public getters to access them.

Upvotes: 4

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Your Student class must implement the Comparable interface in order to use Arrays#sort passing Student[] array. The fact that your class currently have a compareTo method doesn't mean it implements this interface, you have to declare this:

public class Student implements Comparable<Student> {
    //class definition...
}

Upvotes: 5

Related Questions