0

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Eric
  • 946
  • 2
  • 12
  • 25

1 Answers1

1

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

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)

user253751
  • 57,427
  • 7
  • 48
  • 90
  • I appreciate the feedback; I'd been seeing that most people did the one-line reading for inputs, but didn't actually realize that would be essential. I'll change and see what happens. so here is the plan of attack: could I read in the input into a string with the scanner using the nextLine command instead of just next and nextInt, and then parse the string out? is that what you are saying? – Eric Feb 10 '15 at 06:27
  • also, will i need to do ctrl-d enter regardless? Our assignment did not say anything about this, which i feel like she would have explained (however, I only know C and Cpp since I skipped Intro Java for this class, which was sort of a prerec) – Eric Feb 10 '15 at 06:40
  • @Eric Ctrl-D acts the same as getting to the end of a file - so `hasNext` returns false - and it's not specific to Java. Possibly your assignment was expecting you to process one line at a time, or possibly it was expecting you to hard-code a string. If you're not clear then ask someone - this problem isn't directly related to the assignment, so it's not asking them to do your work for you. – user253751 Feb 10 '15 at 06:44