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). 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. 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. 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.