gpt4 book ai didi

java - 我不明白为什么我的 java 扫描仪抛出 "NoSuchElementException"

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:05 25 4
gpt4 key购买 nike

我们需要制作一个词法分析器,而我在使用我的特定函数 useLoad 时遇到了一些麻烦,或者更准确地说,使用 useLoad 后 Main 中发生的情况。

我发现那是因为......出于某种原因,buffer=keyboard.nextLine() 抛出了错误,因为由于某种原因它没有从键盘获得更多输入。我认为 .nextLine() 应该强制它从用户那里获取更多输入。我不知道为什么它会在这个特定方法之后专门抛出该异常。它可以很好地执行其他方法,并且不会失去其阅读能力。是因为我在另一个对象中有一个名为键盘的变量并将其关闭吗?这似乎值得怀疑。刚刚尝试更改名称。没有什么区别。

以下代码中使用但未声明的变量:Keywords[0] 是字符串“load”。初始 = 传递给函数的扫描仪字符串。 offset = 一个计数器变量,用于查看我们已读取的行的距离。

useLoad 函数(我认为这在某种程度上搞砸了)位于底部,但我按时间顺序包含了它运行的所有内容(每个方法由水平规则分隔),以防万一我看不到发生了什么。

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); //the scanner for keyboard
int i = 0;
String buffer ="";
boolean loopControl == true;
SymbolTable mySym = new SymbolTable();

System.out.println("READY FOR INPUT\n");

while (loopControl == true){
//read in the line
buffer = "";
buffer = keyboard.nextLine();
if(!mySym.checkStop(buffer)){ //if we didn't stop
mySym.primary(buffer);
}
else{//if we did stop
closeLoop();
}

if (i >= 55){
loopControl = false;
System.out.println(("You have gone over the limit ("+i+" lines) per execution. Please continue by running this program again.").toUpperCase());
//just a safety precaution...you know... in case closeLoop doesn't work
}
i++;
}


keyboard.close();

}
<小时/>
    if(initial.substring(0, Keywords[0].length()).equals(Keywords[0])){ //Load
//if this is working as expected, then we simply need to do what the keyword says to do.
offset += Keywords[0].length(); //we have moved this much deeper in to the line
useLoad(offset, initial);
offset = 0; //just make sure, once we are done with the line, we start back at the start of the next line.
return; //we found what we were looking for, get out.
}
<小时/>
private void useLoad(int offsetIn, String readIn) {
double doubIn = 0;
//now get the value of the
Scanner keyboard = new Scanner(System.in); //the scanner for keyboard
System.out.println("\nENTER VALUE FOR " + readIn.toUpperCase());
doubIn = keyboard.nextDouble();
keyboard.close();

variables.create(readIn.substring(offsetIn), doubIn);
}

最佳答案

我想我已经解决了你的问题。

Java 7 和 8 的 Java 文档在 Scanner 的 close 方法文档中都包含这一行:

If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked.

查看 System 的文档,我发现 System.in 是 InputStream 类型,您猜对了,它实现了 CloseableInputStreamclose 方法文档说它什么也不做;但是,InputStreamabstract,而close标记为final,这意味着它可以被覆盖。 System.in 返回一个 InputStream,它可能 - 并且显然确实 - 执行某些操作。

所以问题是,您正在使用 System.in 创建多个 Scanner,并且每次关闭其中任何一个时,您都会关闭 System.in,使其变得无用!

这个问题其实在另一个问题here中已经讨论过,并给出解决方案。也就是说,对于您的程序,我建议采用以下两种方法之一:

  • 那里提到了第一种方法:要么使用预制的包装类,要么创建自己的包装类,它在其构造函数中接受 InputStream 。让此类的 InputStream 实现调用其包装对象的所有方法(除了禁止销售的 close 方法),然后将 Wrapper(System.in) 传递给 Scanner,而不是直接将 System.in 传递给 Scanner。但是,除非在非常特殊的情况下,否则我会对采用这种方法持谨慎态度,因为每当您使用这些包装器之一时,您都需要记住在使用结束时关闭其包装对象,除非它类似于System.in
  • 第二种方法:在程序中使用运行程序类,并在那里初始化扫描器。在构造过程中将扫描仪传递给所需的对象,以便它们保存引用,然后允许它们完成所需的任务,而无需关闭类中的扫描仪。指定退出条件后,返回运行程序类并从那里关闭扫描仪。

关于java - 我不明白为什么我的 java 扫描仪抛出 "NoSuchElementException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39782269/

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