This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Doubt on a program using Scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt on a program using Scanner" Watch "Doubt on a program using Scanner" New topic
Author

Doubt on a program using Scanner

Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
Hi,

I ran the following code to understand how Scanner works.




The following is a sample output for the above code

Keep going? (Y or N) 6

Keep going? (Y or N) g

Keep going? (Y or N) n




Am not able to understand how the program re-ran when i entered 6 as input first time
and pressed Enter.. According to me it should run just once.

Thanks.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8661
    
  23

Rajiv Rai wrote:Am not able to understand how the program re-ran when i entered 6 as input first time
and pressed Enter.. According to me it should run just once.

Why? What do you think while(true) does?

Tip: Add
else System.out.println("Invalid response");
at line 16, and see what it produces.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
jQuery in Action, 3rd edition
 
subject: Doubt on a program using Scanner
 
It's not a secret anymore!