• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why isn't my control statement executing properly?  RSS feed

 
Dustin Boor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New to java here...when I run my example program, if the user's age is over 18, why is my program ending immediately after it asks the user if they have ever voted before? The curser should go to the next line for input which it doesn't.

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at how Campbell explained the problem and solution here: https://coderanch.com/t/463484/java/explanation-getter-setter-methods-difference#2074932
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also added this question to the Java Beginners FAQ since it seems to come up often enough to qualify as "frequently asked".
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might actually have something to do with the weird behavior of the Scanner Class. I believe that when you read input from an integer, and then afterwards from a String, it skips over the String. A test for instance could be to test again with another "int-based question". Ask the user something like how many times have they voted before instead and see if it goes through. Have the Scanner object read in an int as opposed to a String.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naziru Gelajo wrote:This might actually have something to do with the weird behavior of the Scanner Class. I believe that when you read input from an integer, and then afterwards from a String, it skips over the String. A test for instance could be to test again with another "int-based question". Ask the user something like how many times have they voted before instead and see if it goes through. Have the Scanner object read in an int as opposed to a String.

I suppose you could speculate and experiment... or you could follow the links provided above to the explanations that already exist  ¯\_(ツ)_/¯
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a direct link to the long answer that is referenced in the Java Beginners FAQ: https://coderanch.com/t/676482/Scanner-nextLine-gotcha
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can check out the Similar threads list on this page, below.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Here's a direct link to the long answer that is referenced in the Java Beginners FAQ: https://coderanch.com/t/676482/Scanner-nextLine-gotcha


Very nice explanation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!