0

What I am trying to do is have the user enter a lot of numbers and then hit enter, and then store all those numbers onto a stack at once. My thought was to use a loop to go through all the numbers and push them onto the stack like so:

Stack<Integer> mainBin = new Stack<Integer>();

Scanner scanner = new Scanner(System.in);

while (scanner.hasNextInt()) {          
mainBin.push(scanner.nextInt());
}

However, even after I press enter many times without entering anything new, it still stays in the loop. Any suggestions?

8
  • Well this is literally all the code I have so far. I ran into this problem doing another program and was wondering what was happening, so I am just trying to figure out the problem by itself. No more code than this is needed. Just want to know why loop keeps going, or what is a better way to achieve same results? Commented Nov 5, 2015 at 23:03
  • 2
    Possible duplicate of How to use .nextInt() and hasNextInt() in a while loop Commented Nov 5, 2015 at 23:19
  • @sam Since you seem to be very new to Java: ideone uses a different InputStream for simulating user input and it behaves very differently, so trying to reproduce this question with ideone won't help here. Commented Nov 5, 2015 at 23:21
  • @Tom All I saw was infinite loop without checking other details. Thanks for pointing out. I am new :) Commented Nov 5, 2015 at 23:23
  • I don't think it uses a different InputStream -- it's just that the program is executed with input from a pipe rather than connected to an interactive terminal, which means that the input automatically yields EOFs when it's over. The lack of which is exactly @Chaus' problem. What I'm trying to say: the difference that makes it work as expected in Ideone is not in Java, but rather the execution environment. Commented Nov 5, 2015 at 23:24

2 Answers 2

1

Scanner.hasNextInt() skips over whitespace to find the next token. So it reads and skips over all your Enter presses. Then it waits for more input, because there may be more input coming.

In a Linux terminal, you can press Ctrl-D (maybe Cmd-D on OS X?) to send an end-of-file marker, which tells the application that there is no more input coming (this should be done at the start of a line). This answer suggests that Ctrl-Z is the equivalent in Windows' command prompt.

Alternatively, you could have some special input that your application reads. @phatfingers commented that you could specify the number of values to read as the very first input. You could also have a special value to signify the end (0 or -1 are common choices, based on the application's needs), or maybe even use a non-numeric token like "end".

Sign up to request clarification or add additional context in comments.

1 Comment

Just to clarify, on the Mac, you still use Ctrl-D to send the end-of-file marker.
0

Your example uses scanner.hasNext(). Use scanner.hasNextInt() instead.

3 Comments

Sorry, that was me trying to just test and see if it would change things. I am using hasNextInt(), and have edited my post to show this, but I still receive the same results.
Okay, I just noticed your "press enter many times" comment. Both hasNext() and hasNextInt() will block waiting for input. You could put your input into a file and substitute a new File(filename) for System.in, or have your loop inspect the content for something specific to break out of the loop.
One common technique (on HackerRank, for example) is to specify the number of values to read as the first integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.