سؤال

I'm working on a class assignment that accepts last name, first name and score for one or more students, stores them in an array and then sorts alphabetically by last name (or first name if last name is the same). We're required to use a Student class that implements the Comparable interface. As soon as I get to the Arrays.sort portion of the code, I get a ClassCastException stating that "Student cannot be cast to java.lang.Comparable". I've searched and reviewed, tried to use implements Comparable<Student>, Clean and Build, but the error persists. Any help or hints are appreciated.

the error:

Exception in thread "main" java.lang.ClassCastException:
studentscoreapp.Student cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at studentscoreapp.StudentScoreApp.main(StudentScoreApp.java:35)
Java Result: 1 BUILD SUCCESSFUL (total time: 12 seconds)

Here's my Student class:

public class Student implements Comparable
{

private String lastName;
private String firstName;
private int score;

public Student(String fName, String lName, int s)
    {
    fName = firstName;
    lName = lastName;
    s = score;
    }

public String getLastName()
    {
    return lastName;
    }

public void setLastName(String lastName)
    {
    this.lastName = lastName;
    }

public String getFirstName()
    {
    return firstName;
    }

public void setFirstName(String firstName)
    {
    this.firstName = firstName;
    }

public int getScore()
    {
    return score;
    }

public void setScore(int score)
    {
    this.score = score;
    }

@Override
public int compareTo(Object obj)
    {
    Student sent = (Student) obj;
    if (sent.lastName.equals(this.lastName))
    {
    return this.firstName.compareToIgnoreCase(sent.firstName);
    }
    else return this.lastName.compareToIgnoreCase(sent.lastName);
    }

@Override
public String toString()
    {
    return lastName + firstName + score;
    }
}

the Comparable interface:

public interface Comparable
{

int compareTo(Object obj);
}

and my main:

import java.util.Arrays;

public class StudentScoreApp {

public static void main(String[] args)
    {
    String firstName;
    String lastName;
    int score;

    System.out.println("Welcome to the Student Scores Application");
    int numStudents = Validation.getInt("Enter # of Students: ");
    Student[] studentArray = new Student[numStudents];

    int i;
    for (i = 0; i < numStudents; i++)
        {
        firstName = Validation.getString("Student [" + (i + 1) + "] first name: ");
        lastName = Validation.getString("Student [" + (i + 1) + "] last name: ");
        score = Validation.getInt("Student [" + (i + 1) + "] score: ", 0, 100);
        studentArray[i] = new Student(firstName, lastName, score);
        }


    Arrays.sort(studentArray); //line 35

    System.out.println();

    //take each obj of the array and print the student info
    for (Student obj : studentArray)
        {
        System.out.println(obj.toString());
        }

    }
}
هل كانت مفيدة؟

المحلول

Arrays.sort() accepts an array of objects which are subtypes of java.lang.Comparable. In your code, you have created your own Comparable interface, which, while it behaves the same way as java.lang.Comparable, is not the same interface.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top