Большое спасибо за отзыв. Это была моя первая попытка такого рода опубликовать вопрос в Интернете, и вы заставили меня понять, что это работает:).
Я понял, где моя ошибка. Это было в объявлении моего метода compareTo (), где я использовал «C» вместо «c» для CompareTo помимо некоторых изменений в main. Вот оно:
Файл драйвера:
импорт java.util.Scanner;
import java.util.Arrays;
публичный класс Main {
public static void main(String[] args) {
System.out.println("Welcome to the Students Score Application");
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of Students: ");
int num_Students = Validator.getInt(sc);
Student[] stu = new Student[num_Students];
for(int i =0 ; i<stu.length ;i++)
{
int count = 1;
String lname = Validator.getString(sc,"Student " + count + " Last Name: ");
String fname=Validator.getString(sc,"Student " + count + " First Name: ");
System.out.print("Student " + count + " Score: ");
int score= Validator.getIntWithInRange(sc,0,100);
System.out.println();
count++;
stu[i]= new Student(lname,fname,score);
}
Arrays.sort(stu);
for (Student s: stu)
System.out.println(s.getLastName()+ " , " + s.getFirstName()+ ":" + s.getScore());
}
}
Файл Student.java
Публичный класс Студент реализует сопоставимый {
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;
}
public String getLastName()
{
return lastname;
}
public String getFirstName()
{
return firstname;
}
public int getScore()
{
return score;
}
public int compareTo(Object o)
{
Student s = (Student) o;
int comparison;
final int EQUAL = 0;
comparison=this.lastname.compareTo(s.lastname);
if(comparison != EQUAL)
return comparison;
else comparison = this.firstname.compareTo(s.firstname);
if(comparison!= EQUAL)
return comparison;
return EQUAL;
}
}
файл валидатора:
import java.util.Scanner;
Public Class Validator {
public static String getString(Scanner sc , String prompt)
{
/* System.out.print(prompt);
String s = sc.next();
if (s.equals(""))
return "Enter some name"; // read the user entry
else return s ;*/
String s = "";
// Scanner sc1 = новый сканер (System.in);
// System.out.println (prompt);
boolean isValid = false;
while (isValid == false )
{
System.out.print(prompt);
s = sc.nextLine();
if (s.equals("")){
System.out.println("This entry is required. Try again.");
}
else
isValid = true;
}
return s;
}
public static int getInt(Scanner sc)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine();
// discard any other data entered on the line
}
return i;
}
public static int getIntWithInRange(Scanner sc,int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc);
if (i < min)
System.out.println("Error!Number must be greater than " + (min-1) + ".");
else if (i > max)
System.out.println("Error! Number must be less than " + (max+1) + ".");
else
isValid = true;
}
return i;
}
}
Еще раз. Большое спасибо всем.
С уважением,
Kathy