• 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

Simple program not running , InputMismatchException

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is scanner throwing InputMismatchException at line 14 , i don't understand .

Please help

Screenshot-(2183).png
[Thumbnail for Screenshot-(2183).png]
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you have simply hit <return> without entering a number.
 
Gourav Das
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Because you have simply hit <return> without entering a number.



No you are wrong , java doesnt work like that , it will keep blinking as long as he user keeps pressing enter..
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope.

From here:


Throws:
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range



An empty String would class as not matching the Integer regex.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have thought an empty String would match the delimiter, which is multiple whitespace. Let's try the offending code. No, that isn't necessary. The error isn't where we thought it was. It is on line 6 7. Its baleful effects don't become evident until line 14.

Why are you calling the class MonthConverter? It doesn't convert any months to anything.
Don't declare variables earlier than you need them. Delete line 10, and get rid of some of the excess empty space. You now have this:-Look at line 7, which is a better way to declare the variable. But I haven't corrected the original problem.

[edit]Somebody told me I got the line number fr the error wrong: should be 7
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're instantiating your Scanner with a string, "System.in", not the InputStream, System.in. Your call to nextInt() tries to parse the String into a number which obviously will fail since "System.in" does not have anything in it remotely parseable into an int, that's why you're getting the exception.

Basically, you're using the constructor Scanner(String source), not the one you actually wanted, Scanner(InputStream source)
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following works:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic