В моей программе я пытаюсь сравнить имена по фамилии, а если они совпадают, сравните их, используя первое имя. Однако я не могу понять, как сравнивать строки.CompareTo (Object o), используемый для сравнения строк в java
Может ли кто-нибудь помочь мне с этим?
public class Student implements IComparable
{
String firstName;
String lastName;
int score;
public Student()
{
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getFirstName()
{
return firstName;
}
public void getLastName(String lastName)
{
this.lastName = lastName;
}
public String getLastName()
{
return lastName;
}
public void getScore(int score)
{
this.score = score;
}
public int getScore()
{
return score;
}
@Override
public int compareTo(Object o)
{
//Compares Student objects by last name. If the last names are the same
//it compares by first name.
Student s = (Student) o;
if (this.getLastName().toUpperCase() < s.getLastName().toUpperCase())
return -1;
else if (this.getLastName().toUpperCase() > s.getLastName().toUpperCase())
return 1;
else
{
if(this.getFirstName().toUpperCase(< s.getFirstName().toUpperCase()
return -1;
else if (this.getFirstName().toUpperCase(> s.getFirstName().toUpperCase()
return 1;
else
return 0;
}
}
}
Что такое 'IComparable'? В Java «Comparable» является общим. –