从文本文件创建对象时检查无关的空行
Checking for extraneous blank lines when creating objects from a text file
我正在尝试通过读取给定的文本文件来创建 Student 对象,但该文本文件在实际信息下有一堆空白行。我不允许以任何方式编辑文本文件,也不允许使用除扫描仪之外的任何其他方法来读取文件。一切正常,直到我到达文件底部,抛出 NoSuchElementException
并且代码失败。
ArrayList<Student> Students = new ArrayList<>();
while (fileIn.hasNextLine()) {
Student student = new Student(fileIn.next(), fileIn.next(), fileIn.nextInt());
student.setExam1(fileIn.nextInt());
student.setExam2(fileIn.nextInt());
student.setExam3(fileIn.nextInt());
Students.add(student);
fileIn.nextLine();
System.out.println(student.toString());
}
文件看起来像这样
Peck CSI1000 12345 97 76 72
Rhodes CSI1000 87649 98 70 73
Rinke CSI1000 87649 78 70 78
Romanski CSI1000 87649 84 84 95
Rombach CSI1000 12345 82 86 96
Ruan CSI1000 87649 70 94 76
Ruc CSI1000 87649 97 98 80
Scott CSI1000 87649 90 80 73
Shah CSI1000 87649 98 72 71
Teal CSI1000 87649 89 72 99
Tingley CSI1000 87649 76 85 72
Towne CSI1000 87649 71 71 100
Tucker CSI1000 12345 89 71 93
Ureel CSI1000 87649 76 80 99
Wallace CSI1000 87649 76 100 71
Weber CSI1000 87649 75 73 75
Wierszewski CSI1000 12345 93 90 72
Wilmot CSI1000 12345 85 96 74
Wilson CSI1000 12345 91 75 97
Yang CSI1000 87649 83 80 85
Yasoni CSI1000 87649 78 90 76
你可以尝试捕获异常
ArrayList<Student> Students = new ArrayList<>();
while (fileIn.hasNextLine()) {
try{
Student student = new Student(fileIn.next(), fileIn.next(), fileIn.nextInt());
student.setExam1(fileIn.nextInt());
student.setExam2(fileIn.nextInt());
student.setExam3(fileIn.nextInt());
Students.add(student);
fileIn.nextLine();
System.out.println(student.toString());
}catch(NoSuchElementException e){
//break;
fileIn.nextLine();
System.out.println("exception");
}
}
或者如果在异常之后您不期望任何更多信息,您可以 break
在 catch 块中
我正在尝试通过读取给定的文本文件来创建 Student 对象,但该文本文件在实际信息下有一堆空白行。我不允许以任何方式编辑文本文件,也不允许使用除扫描仪之外的任何其他方法来读取文件。一切正常,直到我到达文件底部,抛出 NoSuchElementException
并且代码失败。
ArrayList<Student> Students = new ArrayList<>();
while (fileIn.hasNextLine()) {
Student student = new Student(fileIn.next(), fileIn.next(), fileIn.nextInt());
student.setExam1(fileIn.nextInt());
student.setExam2(fileIn.nextInt());
student.setExam3(fileIn.nextInt());
Students.add(student);
fileIn.nextLine();
System.out.println(student.toString());
}
文件看起来像这样
Peck CSI1000 12345 97 76 72
Rhodes CSI1000 87649 98 70 73
Rinke CSI1000 87649 78 70 78
Romanski CSI1000 87649 84 84 95
Rombach CSI1000 12345 82 86 96
Ruan CSI1000 87649 70 94 76
Ruc CSI1000 87649 97 98 80
Scott CSI1000 87649 90 80 73
Shah CSI1000 87649 98 72 71
Teal CSI1000 87649 89 72 99
Tingley CSI1000 87649 76 85 72
Towne CSI1000 87649 71 71 100
Tucker CSI1000 12345 89 71 93
Ureel CSI1000 87649 76 80 99
Wallace CSI1000 87649 76 100 71
Weber CSI1000 87649 75 73 75
Wierszewski CSI1000 12345 93 90 72
Wilmot CSI1000 12345 85 96 74
Wilson CSI1000 12345 91 75 97
Yang CSI1000 87649 83 80 85
Yasoni CSI1000 87649 78 90 76
你可以尝试捕获异常
ArrayList<Student> Students = new ArrayList<>();
while (fileIn.hasNextLine()) {
try{
Student student = new Student(fileIn.next(), fileIn.next(), fileIn.nextInt());
student.setExam1(fileIn.nextInt());
student.setExam2(fileIn.nextInt());
student.setExam3(fileIn.nextInt());
Students.add(student);
fileIn.nextLine();
System.out.println(student.toString());
}catch(NoSuchElementException e){
//break;
fileIn.nextLine();
System.out.println("exception");
}
}
或者如果在异常之后您不期望任何更多信息,您可以 break
在 catch 块中