Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InputMismatchException not working?

 
Jake Cirino
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my code, I wan't the user to enter integers only but this code doesn't seem to be working.



The error is at about line 30.
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to catch the Exception, then you have to surround the code that takes the Scanner input. Below is the code that reads the input. You have misplaced your try-catch block.

 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't catch an InputMismatchException, you should prevent it:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic