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?
InputStreamfor simulating user input and it behaves very differently, so trying to reproduce this question with ideone won't help here.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.