This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Samuel Arwood
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hint: what happens when you type more stuff on the keyboard?
 
Samuel Arwood
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic