gpt4 book ai didi

Java 扫描仪错误 : Exception in thread "main" java. util.NoSuchElementException:找不到行

转载 作者:行者123 更新时间:2023-12-04 04:42:07 25 4
gpt4 key购买 nike

我正在尝试获取用户输入,但我不断收到错误消息:找不到行
行号引用“input = fileS.nextLine();”作为错误的来源

System.out.print("Is this table a simple table? Please check document to confirm, If YES please Enter Y If NO please Enter N \n");
Scanner fileS = new Scanner(System.in);

input = fileS.nextLine();
input = input.trim();
input = input.toLowerCase();

tableCount ++;
fileS.close();

那是我的代码,我知道如果我使用 fileS.hasNextLine() 它将避免此错误

这会一起跳过用户输入。

我在这里想念什么?

扫描仪处于公共(public)功能

提前致谢!

最佳答案

您应该以这种方式阅读输入:

while(fileS.hasNextLine())
{
//Your operations here
}

当你这样做

fileS.nextLine();



它将尝试读取,如果没有找到行,它将抛出异常。也找

fileS.close()



在阅读之前在您的代码文件中。如果在读取操作之前关闭它,则不能再次打开它,它会抛出异常。我认为这是最可能的原因,因为我几周前就有了:P

关于Java 扫描仪错误 : Exception in thread "main" java. util.NoSuchElementException:找不到行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18725842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com