不明白如何解决 noSuchElementException?
Can't understand how to solve noSuchElementException?
我正在尝试编写一个程序来读取一个名为 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
开头的学生人数是 3,课程人数是 4(是的,我知道这很愚蠢,但文件是为我的任务提供的). c081等c号是学校的课程编号,s2023等s号是学号。中间的数字代表他们的分数,-1也代表他们没有被录取。
无论如何,到目前为止我已经写了一个 MySchool
class 和一个 Student
class,我将粘贴在下面:
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)
如果有任何帮助,三个学生代码和他们的分数会在我收到此错误之前按应有的方式打印出来。
任何人都可以帮我解决这个问题 运行ning 吗?我不确定如何解决它
编辑:
我实际上注意到这个问题是,当数据中没有 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);
}
}
问题在于您实际上是在读取字符的 ASCII 代码,而不是值本身 - '3' == 51
。您需要将字符转换为正确的值。最简单的方法是使用Character.getNumericValue()
,例如:
char c = headerRow.charAt(0);
numberOfStudents = Character.getNumericValue(c);
我正在尝试编写一个程序来读取一个名为 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
开头的学生人数是 3,课程人数是 4(是的,我知道这很愚蠢,但文件是为我的任务提供的). c081等c号是学校的课程编号,s2023等s号是学号。中间的数字代表他们的分数,-1也代表他们没有被录取。
无论如何,到目前为止我已经写了一个 MySchool
class 和一个 Student
class,我将粘贴在下面:
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)
如果有任何帮助,三个学生代码和他们的分数会在我收到此错误之前按应有的方式打印出来。
任何人都可以帮我解决这个问题 运行ning 吗?我不确定如何解决它
编辑:
我实际上注意到这个问题是,当数据中没有 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);
}
}
问题在于您实际上是在读取字符的 ASCII 代码,而不是值本身 - '3' == 51
。您需要将字符转换为正确的值。最简单的方法是使用Character.getNumericValue()
,例如:
char c = headerRow.charAt(0);
numberOfStudents = Character.getNumericValue(c);