0

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());
        }

    }
}
3
  • The first thing to learn is that the stack trace is the most important piece of diagnostic/forensic information. You should include the stack trace in your question. Commented Nov 9, 2013 at 3:52
  • For future reference, you can/should edit your question to add additional information (i.e., the stack trace). Commented Nov 11, 2013 at 23:16
  • Thanks, Jim. I added the stack trace to the question after having posted it here as a comment. To avoid duplication and confusion, I'm deleting it from the previous comment. Commented Nov 15, 2013 at 3:26

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So, how would I make it so that it uses my interface? (and thank you ;)
@user2971045 You don't. You switch to the java Comparable interface
I guess I need to clarify the assignment requirements with my teacher. We were told to create a Comparable interface, implement it in the Student class and override the compareTo method for our own sort. Thanks for your help, @Tyler and JimN.
@user2971045 It looks like your teacher wants you to implement your own sort then instead of using the built in one.

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.