• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception in thread "main" java.util.InputMismatchException

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm having a bit of an issue, I'm quite stuck.

So, I have this program. The first method gets the input of an integer from the user and then passes it to another method which checks it to be in a range. That works fine. The problem is that if they enter something other than an int such as a double or a string, it causes an exception and the program crashes. Now, I need to handle this, as simple of just making it say what the issue was and then calling the method that requires them to enter the integer again, but I just can't remember how to handle this exception and unfortunately, my searches on google, here, and through my textbook have not been very helpful.



 
Greenhorn
Posts: 22
Mac Netbeans IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe this will help point you in the right direction:

http://docs.oracle.com/javase/tutorial/essential/exceptions/

You may also want to consult the API documentation. When you click on say, java.lang, you will also find a list of exception classes.

Steve
 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, so after looking into it, I get to a point where I can compile, except my exception handling isn't seeming to be working. It doesn't seem to be catching the exception and just returning it to JVM.



 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You are requesting the same input twice. Once before the try once inside it.

Never try handling an InputMismatchException (IME). The right way (in my opinion) to use a Scanner pointing to System.in is inside a utility class. You would have to write your own utility class. You can create a method something likeNow, a long time ago, Rob Spoor taught me that you can always avoid an IME when you use the keyboard by getting the program to ask for a new input and showed me something very similar to the above. A few more suggestions can be found in my old posts if you search them for Scanner nextInt hasNextInt and utility class.

If you create yourself a utility class, you can overload that method to return a value in a certain range only. Use something like while (i < min || i > max) …, and remember you will get a nice infinite loop if you pass max less than min

If you do that, you can be certain to avoid the IME for int inputs, but remember that technique does not work at all well if you are reading from a file. It would need locking if used in a multithreaded environment.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic