我当前正在编写一个程序,该程序将类代码写入文件,然后从所述文件中读取它们并将它们打印到屏幕上。每次运行程序时,我都会不断获取java.util.NoSuchElementException。我进行了每一次修改后,这个问题仍然存在,我不确定从这里去哪里。任何帮助将不胜感激。
package u2a1_readtextfilehandleexcep;
import java.io.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class U2A1_ReadTextFileHandleExcep {
public static void main(String[] args) throws FileNotFoundException, IOException {
//Create new file called course.txt
java.io.File file = new java.io.File("course.txt");
if (file.exists()) {
System.out.println("File already exists; try another name.");
System.exit(0);
}
//Input the specified words to the file
try (PrintWriter output = new PrintWriter(file)) {
output.println("IT2249 6 Introduction to Programming with Java");
output.println("IT2230 3 Introduction to Database Systems");
output.println("IT4789 3 Mobile Cloud Computing Application Development");
}
try (
//Reads from file to the console
Scanner input = new Scanner(file)) {
while (file.canRead()) {
String code = input.next();
int creditHours = input.nextInt();
String courseTitle = input.nextLine();
System.out.println("Course Code = " + code + " | Credit Hours = " + creditHours + " | Course Title = " + courseTitle);
}
input.close();
}
}
}
运行程序后:
Course Code = IT2249 | Credit Hours = 6 | Course Title = Introduction to Programming with Java
Exception in thread "main" java.util.NoSuchElementException
Course Code = IT2230 | Credit Hours = 3 | Course Title = Introduction to Database Systems
Course Code = IT4789 | Credit Hours = 3 | Course Title = Mobile Cloud Computing Application Development
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at u2a1_readtextfilehandleexcep.U2A1_ReadTextFileHandleExcep.main(U2A1_ReadTextFileHandleExcep.java:26)
C:\Users\Deb\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
答案 0 :(得分:1)
一旦没有其他要读取的元素,Scanner
将引发您所看到的异常。现在,您可以继续循环读取文件,但是与Scanner
在文件中的位置无关。 Scanner
提供了一系列功能来检查下一个标记的有效性:hasNext()
,hasNextInt()
,hasNextLine()
等。您应该改用以下这些标记:
while( input.hasNext() ) {
String code = input.next();
// ...
}
但是,如果文件格式错误,出于类似的原因,您仍然可以通过读取小时和标题来获取异常。您可以在读取扫描器之前检查扫描器,或者在异常处理程序中进行处理,因为这可能表示更大的问题,例如读取不受支持或损坏的文件。
答案 1 :(得分:0)
请调用Scanner.hasNext()
,以便在以下情况下终止循环
没有更多的元素。
不要在使用try-with-resources时显式关闭输入 会帮忙的。
不需要两个单独的try块,因为它们可能是
结合在一起,一旦写作完成使用
PrintWriter.flush()
写入磁盘。
package u2a1_readtextfilehandleexcep;
import java.io.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class U2A1_ReadTextFileHandleExcep {
public static void main(String[] args) throws FileNotFoundException, IOException {
//Create new file called course.txt
java.io.File file = new java.io.File("course.txt");
if (file.exists()) {
System.out.println("File already exists; try another name.");
System.exit(0);
}
//Input the specified words to the file
try (PrintWriter output = new PrintWriter(file);Scanner input = new Scanner(file)) {
output.println("IT2249 6 Introduction to Programming with Java");
output.println("IT2230 3 Introduction to Database Systems");
output.println("IT4789 3 Mobile Cloud Computing Application Development");
output.flush();
while (file.canRead() && input.hasNext()) {
String code = input.next();
int creditHours = input.nextInt();
String courseTitle = input.nextLine();
System.out.println("Course Code = " + code + " | Credit Hours = " + creditHours + " | Course Title = " + courseTitle);
}
}
}
}
答案 2 :(得分:0)
file.canRead()
将始终返回true,并且当读取指针到达EOF时,下一个input.read()
操作将失败。
使用input.hasNextLine()
检查是否达到EOF。