Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on a program using Scanner

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic