Two Laptop Bag*
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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: 7718
    
  20

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on a program using Scanner