Two Laptop Bag*
The moose likes Beginning Java and the fly likes Can someone please tell me why I get stuck in this while loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone please tell me why I get stuck in this while loop?" Watch "Can someone please tell me why I get stuck in this while loop?" New topic
Author

Can someone please tell me why I get stuck in this while loop?

Samuel Arwood
Greenhorn

Joined: Jul 08, 2012
Posts: 6
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.

Enter heap elements: 1 2 3 4 5 6
true
1
true
2
true
3
true
4
true
5

If someone can please shine some light on my problem I would be very grateful.

Thank you
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40


hint: what happens when you type more stuff on the keyboard?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Samuel Arwood
Greenhorn

Joined: Jul 08, 2012
Posts: 6
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.
Samuel Arwood
Greenhorn

Joined: Jul 08, 2012
Posts: 6
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.
Branden Bobo
Greenhorn

Joined: Jul 16, 2012
Posts: 15
Henry Wong wrote:
hint: what happens when you type more stuff on the keyboard?


just curious... I didn't start the thread, and see he solved his problem but what prevented his original code from working?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
@Brendon - I can see two problems in initial code.

1. The last entry of the scanner token (6 in 1 2 3 4 5 6) hangs without a trailing token delimiter to consider it as a separate token.
2. The while loop waits till it gets either a non-integer token.

@Samuel Arwood - You can set the delimiter to pick 6 in the first initial code - in.useDelimiter("[\\s\n]");
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can someone please tell me why I get stuck in this while loop?
 
Similar Threads
Problems w/ a HashMap
User input into array
String Input and Bubble Sort
do while strange problem
String array