We are doing trivial coding as part of an algorithm analysis class and for some reason I can't even get the code down to move on to the next part. Here is my initial while loop to take in a list of integers and add them to an arraylist.
Here is what the console returns when I run what I have so far.
If I type integers and hit enter it will take them as a new input and if I give it non-integer and press enter it ends the loop and moves on. I know I am missing something obvious, but i have been staring at this for like 2 hours and can't figure the problem.
If I type . and hit enter it says false and then 6. Why is it doing this?
With this code I enter "1 2 3 4 5 6 ." and I get returned the correct response. How do I make it so that the user doesn't have to hit space and then period? I remember doing this before but am having a complete memory blanking.
Joined: Jul 08, 2012
After a lot of searching of the google machine I decided to go with reading in the input as a string and then splitting the input and parsing out the integers. In case anyone stumbles across this thread. Thanks.
John Jai wrote:@Samuel Arwood - You can set the delimiter to pick 6 in the first initial code - in.useDelimiter("[\\s\n]");
I think Samuel is also under the impression that the Scanner(InputStream) will do one line, and return no more tokens. The Scanner that take the inputstream will read until the end of stream is reached, and not just a single line. For what the OP wants, it may be better to just read the line from standard input manually, and then use the scanner that takes a string to process the line.