This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons 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: 7549
    
  18

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt on a program using Scanner
 
Similar Threads
can anyone help me with these exam question?
two puzzling errors
Help.
class CLASS and Scanner class
Need help ending number guessing game and asking if user wants to play again