This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes do while strange problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "do while strange problem" Watch "do while strange problem" New topic
Author

do while strange problem

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I've created a do while loop that does insert items into an array list like I want it to but the problem is the output to console. I want the user to enter items into an array so I have a prompt like so:



As you notice it does not print Enter a[2]: to the correct part of the screen, it's like it skips the second iteration to print the prompt yet it still stores to in array[2]. Here is my code:

Deepika Chandrasekaran
Greenhorn

Joined: Apr 06, 2012
Posts: 1
Hi Dustin,

Please use in.hasNextLine() in while loop instead of in.hasNextInt().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Welcome to the Ranch

I am afraid hasNextLine() will probably not help. It will not pick up the letter, and you can still have a next line which is empty.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I ended up doing this and it worked.. sort of.

The problem is the next part of my code is picking up the letter x that is typed in last to end the while loop and runs the last else statement to throw back x is not an option!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You need to write down the boolean expression after while (... on a sheet of paper and explain to yourself exactly what it means.
You would do well to find the String.toUpperCase() and toLowerCase() methods.
You would do well to find about how to use a while loop and the hasNextInt method of Scanner to validate int input.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do while strange problem