aspose file tools*
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
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: 7064
    
  16

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?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on a program using Scanner
 
Similar Threads
Help.
can anyone help me with these exam question?
two puzzling errors
class CLASS and Scanner class
Need help ending number guessing game and asking if user wants to play again