• Post Reply Bookmark Topic Watch Topic
  • New Topic

InputMismatchException Error  RSS feed

 
James Allen A.
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I matched up all my data types correctly but I'm getting the following error.

Here is my code:

I can't figure this out.
 
Robert Lippens
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your stack trace shows the error emerges at line 29, which is where you have morecoins = myScanner.nextBoolean();

It states that this is an InputMismatchException, meaning that whatever the user has input cannot, in your case, be translated into a boolean value. Have you tried to have the user put in true or false? That should solve the issue.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


From the API docs
#hasNextBoolean()
Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false". The scanner does not advance past the input that matched.
nextBoolean()
Scans the next token of the input into a boolean value and returns that value. This method will throw InputMismatchException if the next token cannot be translated into a valid boolean value. If the match is successful, the scanner advances past the input that matched.


Hint:
What are the choices given to the user and what does the method expect to be able to parse to a boolean?

Drat! Robert beat me to it
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch, Robert Lippens
 
James Allen A.
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh I see. I didn't expect it to be that simple of a fix. I, it looks like incorrectly, remember reading that Boolean values could be true, false, yes, no, y, n, etc..

I changed my variable to a String and now use the .equals() function to ask the user for more input. I get the feeling this isn't the best way though.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!