Reputation: 55
I keep getting Exception in thread "main" java.lang.ClassCastException: studentscoresapp.Student cannot be cast to java.lang.Comparable am not able to figure out why. Maybe someone can take a look and tell me where I am messing up. I have been messing with it for hours and it seems that I am making the program worse and nothing I have found online seems to work. Thanks
public class Student implements IComparable<Student>
{
private String lastName;
private String firstName;
private int score;
public Student(String lastName, String firstName, int score)
{
this.lastName = lastName;
this.firstName = firstName;
this.score = score;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the score
*/
public int getScore() {
return score;
}
/**
* @param score the score to set
*/
public void setScore(int score) {
this.score = score;
}
@Override
public int compareTo(Object obj)
{
Student i = (Student) obj;
if (i.lastName.equals(lastName)) {
return firstName.compareToIgnoreCase(i.firstName);
} else {
return lastName.compareToIgnoreCase(i.lastName);
}
}
}
My interface for compareTo override
public interface IComparable<E> {
int compareTo(Object object);
}
and the student score app
public class StudentScoresApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Welcome message
System.out.println("Welcome to the Student Scores Application.");
System.out.println();
int count = ConsoleValidator.getInt("Enter number of students to enter: ", 0);
Student[] student = new Student[count];
for (int j = 0; count > j; j++)
{
student[j] = getItem();
System.out.println();
}
Arrays.sort(student);
for (Student i : student)
{
System.out.println(i.getLastName() + " " + i.getFirstName() + ": " + i.getScore() + "\n");
}
}
public static Student getItem()
{
String name = ConsoleValidator.getString("Student first name: ");
String last = ConsoleValidator.getString("Student last name: ");
int score = ConsoleValidator.getInt("Student score: ", 0);
Student j = new Student(name,last,score);
return j;
}
}
Upvotes: 0
Views: 3043
Reputation: 327
How do you want sort the students, Here's how we can sort the Student according to their scores
First,Convert Student class into List<Student<String, String, Integer>> studentList
and then use this piece of code
Collections.sort(studentList, new Comparator<Student>()
{
public int compare(Student c1, Student c2)
{
if (c1.getScore() < c2.getScore())
return -1;
if (c1.getScore() > c2.getScore())
return 1;
return 0;
}
});
this will sort sort studentList list in ascending order according to the scores.
Upvotes: 2
Reputation: 51711
Although, you should have been using the standard Comparable
interface the way you've written your own is also flawed. It tries to use generics but then gives it up while declaring the method. The correct implementation should have been
public interface IComparable<T> { // Use of T for types recommended
int compareTo(T object); // Use the generic type for method as well
}
Using the standard java.lang.Comparable
interface too, you should be using generics like
public class Student implements Comparable<Student> {
// ...
public int compareTo(Student obj) { // No need for casting now
if (obj.lastName.equals(lastName)) {
return firstName.compareToIgnoreCase(obj.firstName);
} else {
return lastName.compareToIgnoreCase(obj.lastName);
}
}
}
Upvotes: 1
Reputation: 33
For the method Array.sort(), there is a description as:
'All elements in the array must implement the Comparable interface'.
You create the interface IComparable instead of Comparable.
I think it is not correct.
I change the interface to Comparable and it can work.
Any comments thanks for sharing.
Regards,
Hanks.
Upvotes: 1
Reputation: 4184
The exception probably comes from this line.
Arrays.sort(student);
Internally Arrays.sort() will attempt to cast elements of the passed array to Comparable, this so it can call their compareTo() method. That is why you get a class cast exception.
Student must implement Comparable. Don't use your own IComparable interface, instead use java.lang.Comparable.
Upvotes: 8