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?

Chaus
  • 77
  • 1
  • 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? – Chaus Nov 05 '15 at 23:03
  • 2
    Possible duplicate of [How to use .nextInt() and hasNextInt() in a while loop](http://stackoverflow.com/questions/26566773/how-to-use-nextint-and-hasnextint-in-a-while-loop) – Tom Nov 05 '15 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. – Tom Nov 05 '15 at 23:21
  • @Tom All I saw was infinite loop without checking other details. Thanks for pointing out. I am _new_ :) – sam Nov 05 '15 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. – Snild Dolkow Nov 05 '15 at 23:24
  • @sam No worries, I see this often here that some user try to reproduce problems with `System.in` an pages likes ideone, but this rarely work. So it mostly easier to try it on a local IDE. – Tom Nov 05 '15 at 23:25
  • @SnildDolkow Well, you obviously didn't understand my comment, but that's ok, your text is still correct. – Tom Nov 05 '15 at 23:30
  • @Tom Mind explaining in what way I failed to understand your comment? I don't see it, but welcome the opportunity to learn. – Snild Dolkow Nov 06 '15 at 06:26

2 Answers2

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

Community
  • 1
  • 1
Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
  • 1
    Just to clarify, on the Mac, you still use `Ctrl-D` to send the end-of-file marker. – Ben Nov 15 '15 at 12:34
0

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

phatfingers
  • 9,770
  • 3
  • 30
  • 44
  • 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. – Chaus Nov 05 '15 at 23:02
  • 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. – phatfingers Nov 05 '15 at 23:10
  • One common technique (on HackerRank, for example) is to specify the number of values to read as the first integer. – phatfingers Nov 05 '15 at 23:13