我目前正在让该程序读取文本文件的内容,并计算所取考试成绩的平均值和数量,并将其整齐地打印在一个小的数据表中。这些是姓名,测验数量和每个学生的平均成绩:

James Tiberius Kirk              8                         91.63
Buffy Summers                    7                         83.14
Tom Baker                       15                        100.00
Malcolm Reynolds                 9                         84.22
Elizabeth Bennet                 9                         93.33
John Blutarsky                   9                          0.00
Dorthy Gale                      6                         85.83


所有这些Student都存储在名为Anames[]的数组中。我想知道是否有可能使用我现在的代码按姓氏字母顺序对这些学生进行排序。当我运行程序时,它给了我错误:

线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1

at java.lang.String.substring(String.java:1927)
at text.reader.TextReader.compareLastNames(TextReader.java:117)
at text.reader.TextReader.main(TextReader.java:94)


这是我的主要课程的代码:

public static void main(String[] args)throws IOException{

    Double score=0.0;
    int b,j;
    String tempfirst = "";
    String templast = "";
    Student Anames[] = new Student[30];
    Student Temp[] = new Student [1];
    int Stucount = 0;
    Scanner reader = new Scanner(new File("quizScores.txt"));
    boolean runProgram = true;
    PrintWriter writer = new PrintWriter(new File("scoreReport.txt"));
    //prints header for report
    System.out.println("Name                        Number Quizes             Quiz Socres");
    writer.println("Name                        Number Quizes             Quiz Socres");

    //check to see if end of file string
    while (!reader.hasNext("-10")){
        String name="", first="", last="";

        //gets the name from file
        while(!reader.hasNextDouble()){
            last = reader.next();

            while (!reader.hasNextDouble()){
                first = first+reader.next()+" ";
            }
            name=first+last;
        }

        //creates new student with given name
        Student newStudent = new Student(first, last);
        Anames[Stucount] = newStudent;
        Stucount++;

        //gets the quiz scores and makes sure does not averge in the end of file string.
        while (reader.hasNextDouble()&& !reader.hasNext("-10")){
           newStudent.addQuiz(reader.nextDouble());
        }

        //Prints out the formated data
        System.out.printf("%-30s%4.0f%30.2f \n",newStudent.getName(), newStudent.getQuizNumber(), newStudent.getAverage());
        writer.printf("%-30s%4.0f%30.2f",newStudent.getName(), newStudent.getQuizNumber(), newStudent.getAverage());

        writer.println();
    }
    System.out.println("\n");

    for (b = 0; b < Stucount; b++){
        int INTEGERTEMP = b;
        for (j= b+1; j < Stucount; j++){
            int INTEGERTEMP2 = j;
            if ((compareLastNames(Anames[INTEGERTEMP].getLAST(), Anames[INTEGERTEMP2].getLAST()))>0){
                Temp[0] = Anames[b];
                Anames[b] = Anames[j];
                Anames[j] = Temp[0];
            }
        }
    }

    System.out.println("Name                        Number Quizes             Quiz Socres");
    for (int i = 0; i < Stucount; i++) {

            System.out.printf("%-30s%4.0f%30.2f \n", Anames[i].getName(), Anames[i].getQuizNumber(), Anames[i].getAverage());

    }

    writer.close();
}

private static int compareLastNames(String a, String b){
    int index_a = a.lastIndexOf(" ");
    String surname_a = a.substring(index_a);
    int index_b = b.lastIndexOf(" ");
    String surname_b = b.substring(index_b);
    int lastNameCmp = surname_a.compareToIgnoreCase(surname_b);
    return lastNameCmp;
}


这是Student.java,其中包含大多数使用的方法:

public Student (String inName, String inLast){
    studentName=inName;
    studentLast = inLast;
    quizAverage = 0;
    quizScore=0;
    numberQuizes=0;
}

public void addQuiz(double inQuiz){
    quizScore += inQuiz;
    numberQuizes++;
}

public double getAverage(){
    quizAverage = quizScore/numberQuizes;
    return quizAverage;
}

public String getName(){
    return studentName+studentLast;
}

public double getQuizNumber(){
    return numberQuizes;
}

public String getLAST(){
    return studentLast;
}

最佳答案

您可以使用java.util.Arrays.sort(Student [] arr, Comparator<Student> comp)代替自己的比较代码。在单行中,您可以这样实现:

Student arr[];//considering this array you will populate
Arrays.sort(arr,new java.util.Comparator<Student>(){

            public int compare(Student o1, Student o2) {

                return o1.studentLast.compareTo(o2.studentLast);
            }

        });
//then the arr will be sorted with studentLast name

关于java - 在Java中按姓氏对学生数组进行姓氏排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33490976/

10-12 12:29
查看更多