Java 即使在catch中使用NextLine,来自scanner的用户输入也会导致try/catch块中的无限循环
如果输入了正确的文件名,则没有问题。如果输入的文件名不正确,无限循环会不断告诉用户输入文件名。但它不会等待用户输入文件名。它在无限循环中不断处理第一个错误数据我正在catch块中使用nextLine,该块用于清除错误输入。还尝试了重置()。我做错了什么Java 即使在catch中使用NextLine,来自scanner的用户输入也会导致try/catch块中的无限循环,java,file,java.util.scanner,Java,File,Java.util.scanner,如果输入了正确的文件名,则没有问题。如果输入的文件名不正确,无限循环会不断告诉用户输入文件名。但它不会等待用户输入文件名。它在无限循环中不断处理第一个错误数据我正在catch块中使用nextLine,该块用于清除错误输入。还尝试了重置()。我做错了什么 public static String reader() { boolean fileCorrect = false; // Holds the data from the file and is returned.
public static String reader() {
boolean fileCorrect = false;
// Holds the data from the file and is returned.
StringBuilder fromFile=new StringBuilder("");
// Loop until the user enter's a filename that the system can find.
do
{
System.out.println("\nEnter filename to open: ");
//Try with resources to open Scanner object for keyboard input
try (Scanner keyboard = new Scanner(System.in))
{
String fileName = keyboard.next();
// Trim leading/trailing spaces from filename
fileName = fileName.trim();
// The file object opened with try below
File iFile = new File(fileName);
//Attempt to open the file, which is automatically closed by try with resources.
try (Scanner fileInput = new Scanner(iFile))
{
//Read the file and append data to the string.
while (fileInput.hasNextLine())
{
fromFile = fromFile.append(fileInput.nextLine());
}
//If we make it this far, no need to loop.
fileCorrect = true;
}
// Catch specific first, a child of IOException. Most likely to happen.
catch (FileNotFoundException ex)
{
System.err.println(ex.getMessage() + ": File not found");
//ex.printStackTrace();
keyboard.nextLine();
}
// If the scanner somehow closed before data was read.
catch (IllegalStateException ex)
{
System.err.println(ex.getMessage() + ": Error reading from file");
//ex.printStackTrace();
keyboard.nextLine();
}
// Something else went wrong. Generic catch-all to keep the program from crashing.
catch (Exception ex)
{
System.err.println(ex.getMessage());
//ex.printStackTrace();
keyboard.nextLine();
}
}
catch (Exception ex)
{
// handle these
}
}
while (!fileCorrect);
在程序中使用带有break关键字的if-else语句。如果输入的文件名不正确,请使用break关键字,这样它就不会继续无限块 e、 g
您的问题是第一个
try
块。将其更改为:
try (Scanner keyboard = new Scanner(System.in)) {
为此:
try {
Scanner keyboard = new Scanner(System.in);
第一个将继续尝试读取文本,失败并重试,等等,因为
扫描仪已关闭,并且无法按照@ScaryWombat在其评论中所说的方式读取文本移动此扫描仪键盘=新扫描仪(System.in)代码>在执行while循环之前执行
如果您使用的是try(扫描器键盘=新扫描器(System.in))
-它将自动调用System.in扫描器上的close,并且下一步将不起作用
如果你有一个合适的异常处理程序
catch (Exception ex)
{
ex.printStackTrace();
}
你会看到消息的
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at Main.main(Main.java:38)
当您输入错误的文件名时,它应该做什么?如果您有try(Scanner keyboard=new Scanner(System.in)),也不要默默地接受异常catch(Exception ex){//handle this}
-它不会自动调用系统上的close。在
扫描仪中?您能接受回答吗?如果没有合适的,请留下一些反馈,以便我们知道需要修复/改进什么。非常感谢。
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at Main.main(Main.java:38)