无法理解如何解决noSuchElementException吗?

问题描述 投票:0回答:1

[我正在尝试编写一个程序来读取名为scores.txt的文件,该文件将打印出其课程中平均成绩最高的学生的ID以及学生ID。

这是scores.txt的样子:

34     c081 c082 c083 c084
S2023  99   75   85   62
S2025  -1   92   67   52
S1909  100  83   45   -1

所以基本上,34 a的开头是学生人数的3和课程的数量4(是的,我知道这很愚蠢,但是文件是为我的任务提供的)。 c编号(例如c081)是学校的课程代码,s编号(例如s2023)是学生编号。中间的数字代表他们的分数,-1表示他们还没有加入该单元。

无论如何,到目前为止,我已经写了一个MySchool类和一个Student类,我将在下面粘贴:

import java.io.File;
import java.io.FileNotFoundException; 
import java.util.ArrayList;
import java.util.Scanner; 

public class MySchool {
    public static void main(String[] args) {
        int numberOfStudents;
        ArrayList<Student> allStudents = new ArrayList<Student>() ;

        // grab first line only, and check if first target is integer, or string
        try {

            File scoresFile = new File("Scores.txt");
            Scanner scoresFileReader = new Scanner(scoresFile);

            String headerRow = scoresFileReader.nextLine();
            numberOfStudents = headerRow.charAt(0);

            while(scoresFileReader.hasNextLine()) {
            for (int studentI = 0; studentI < numberOfStudents; studentI++) {
                String studentText = scoresFileReader.nextLine();
                System.out.println(studentText);
                Student student = new Student(studentText);
                allStudents.add(student);
            }}

            scoresFileReader.close();

        } catch (FileNotFoundException e) {
            System.out.println("An error occurred");
            e.printStackTrace();
        }

        float highestAverage = 0;
        String highestScoringStudentNumber = null;

        for (Student student : allStudents) {
            if (student.getAverageScore() > highestAverage) {
                highestAverage = student.getAverageScore();
                highestScoringStudentNumber = student.getStudentNumber();
            }

        }
        System.out.println("Highest scoring student: " + highestScoringStudentNumber);
    }
}
import java.util.ArrayList;

public class Student {

    private String studentNumber;
    private ArrayList<Integer> scores = new ArrayList<Integer>();

    public Student(String studentText) {
        String[] parts = studentText.split(studentText, ' ');
        this.studentNumber = parts[0];

        for (int i = 1; i < parts.length - 1; i++) {
            scores.add(Integer.parseInt(parts[i + 1]));
        }
    }

    public String getStudentNumber() {
        return this.studentNumber;
    }

    public float getAverageScore() {
        int sum = 0;
        for (int i = 0; i <this.scores.size(); i++) {
            sum += this.scores.get(i);
        }
        return sum / this.scores.size();
    }


}

[基本上,我希望能够为学生提供一个对象,其中有他们的学生编号和分数。这样我就可以给他们一个平均值。

但是,似乎我在读取文件时做错了(以前从未做过,因为String studentText = scoresFileReader.nextLine();行引发了一个错误,指出:Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at MySchool.main(MySchool.java:22)

[如果有帮助,三个学生代码及其分数将以我应该收到的错误信息的方式打印出来。

有人可以帮助我启动并运行它吗?我不确定如何解决它

编辑:

我实际上已经注意到问题是,当数据中没有51时,numberOfStudents会以某种方式设置为51。即使我运行下面的代码,它也会打印出headerRow的第一个值,确认为3,这是正确的。当我使用相同的代码将其突然分配给numberOfStudents时,再次打印时它变成51?]

import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.ArrayList;
import java.util.Scanner; // Import the Scanner class to read text files

public class MySchool {
    public static void main(String[] args) {
        int numberOfStudents;
        ArrayList<Student> allStudents = new ArrayList<Student>() ;

        // grab first line only, and check if first target is integer, or string
        try {

            File scoresFile = new File("Scores.txt");
            Scanner scoresFileReader = new Scanner(scoresFile);


            String headerRow = scoresFileReader.nextLine();

            System.out.println(headerRow.charAt(0));

            numberOfStudents = headerRow.charAt(0);

            System.out.println(numberOfStudents);


            for (int studentI = 0; studentI < numberOfStudents; studentI++) {
                String studentText = scoresFileReader.nextLine();
                System.out.println(studentText);
                Student student = new Student(studentText);
                allStudents.add(student);
            }

            scoresFileReader.close();

        } catch (FileNotFoundException e) {
            System.out.println("An error occurred");
            e.printStackTrace();
        }

        float highestAverage = 0;
        String highestScoringStudentNumber = null;

        for (Student student : allStudents) {
            if (student.getAverageScore() > highestAverage) {
                highestAverage = student.getAverageScore();
                highestScoringStudentNumber = student.getStudentNumber();
            }

        }
        System.out.println("Highest scoring student: " + highestScoringStudentNumber);
    }
}
    

我正在尝试编写一个程序来读取一个名为scores.txt的文件,该文件会在其中打印出其课程中平均成绩最高的学生ID以及学生ID。这就是分数。...

java text-files file-handling nosuchelementexception
1个回答
0
投票

问题出在您实际上是在读取char的ASCII码,而不是值本身-'3' == 51。您需要将字符转换为正确的值。最简单的方法是使用Character.getNumericValue(),例如:]]

char c = headerRow.charAt(0);
numberOfStudents = Character.getNumericValue(c);  
© www.soinside.com 2019 - 2024. All rights reserved.