简体   繁体   English

Postfix评估程序-扫描仪问题?

[英]Postfix Evaluator— Scanner Issue?

I am using a scanner for a postfix expression evaluator and ran into a slight issue. 我将扫描仪用于后缀表达式评估器,但遇到了一个小问题。 When I enter my expression via command line, the expression does not terminate and reaches an infinite loop. 当我通过命令行输入表达式时,表达式不会终止并达到无限循环。 Using print statements and jdb, I was able to deduce that the commands I desire all execute normally and evaluate properly, however, I am not able to figure out why the scanner will not terminate (thus, leaving it in an infinite while loop). 使用print语句和jdb,我可以推断出我想要的所有命令都能正常执行并正确评估,但是,我无法弄清为什么扫描器不会终止(因此,将其置于无限的while循环中)。 I know that the scanner must eat the input and not just make sure it is left, which it should be doing (I believe). 我知道扫描仪必须吃掉输入,而不仅仅是确定它应该被留下(我相信)。 When I enter my scanner as the expression I wish to evaluate and not System.in (for command line input), everything terminates as desired, which is why I am seriously confused. 当我输入扫描仪作为要评估的表达式而不是System.in(用于命令行输入)时,一切都按期望终止,这就是为什么我很困惑的原因。 Has anybody run into this issue before? 有人遇到过这个问题吗? I'll post some terse code snippets below, taken from the main that fails to terminate. 我将在下面发布一些简短的代码片段,这些片段摘自未能终止的主体。

Scanner i = new Scanner(System.in);
System.out.println("Message...");
StackList stack = new StackLinkedList();
while (i.hasNext()) {
    if (input.hasNextInt()) {
        stack.push(i.nextInt());
        continue;
    }
    String op = input.next());
    Integer val_one = stack.pop();
    Integer val_two = stack.pop();
    switch(op) { a switch statement that evaluates based on the operator};
}
input.close();
System.out.println(stack.pop);

Note: I have checked for previous implementations, and I cannot figure out where mine differs. 注意:我已经检查过以前的实现,但无法弄清楚我的不同之处。 Most people who run into these types of issues are not eating the inputs as they go, which I believe my code should be doing. 遇到这类问题的大多数人都不会一eating而就,我相信我的代码应该这样做。 I am entering expressions such as 1 2 + 'Enter' (answer should be 3, and does come out to be 3 when I set the scanner to the string 1 2 +) but when the input is via command line they are not terminating. 我正在输入诸如1 2 +'Enter'的表达式(答案应该为3,并且当我将扫描仪设置为字符串1 2 +时确实为3),但是当通过命令行输入时,它们不会终止。

A Scanner reading from the console will always have more data, because there's always the possibility that you will type some more. 从控制台读取的扫描仪将始终具有更多数据,因为总有可能会键入更多数据。

You could press Ctrl-D Enter (on Linux) or Ctrl-Z Enter (on Windows) which simulates an "end of file" condition on the console (as if you were reading from a file or string and got to the end of it). 您可以按Ctrl-D Enter键 (在Linux上)或Ctrl-Z Enter键 (在Windows上)来模拟控制台上的“文件结束”状态(就像您从文件或字符串中读取并到达文件末尾一样) )。

However, perhaps you want to read one line at a time from the console and then parse each line individually? 但是,也许您想一次从控制台读取一行,然后分别解析每一行? (with the Scanner constructor that takes a String which is the line you read) (使用带StringScanner构造函数,该String是您读取的行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM