Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Catching Exceptions question/problem  RSS feed

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am working on some code and using a Switch statement with a Scanner to get user input.The input needs to be an integer and if it isn't then i want
to catch the InputMismatchException and allow the user to go back to the options menu and try again.



if you look at the catch block i wrote in the error i get, but i thought this is how one should discard the last entry?? How else can i do that?

Thanks
Mike
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read what the error said ? nextLine is not a static method, so you need to call it on an instance of the class.
Scanner.nextLine();
should be
menu.nextLine();
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne,

Thanks you, I read the error but didn't understand what it meant exactly.After seeing the correct way i understand now!!

Thanks
Mike
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Important note: you can prevent the exception. Scanner has methods for checking what's available next. Instead of catching the exception you can test the result of hasNextInt(). Don't forget to consume the input like you're doing now or you'll hit an infinite loop.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Thanks for the tipp!! I will test that out right now, sounds like a good solution!!


Mike
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you worked out what Rob meant yet? If not, ask again, or search, because that question comes up about once every six months.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote:Have you worked out what Rob meant yet? If not, ask again, or search, because that question comes up about once every six months.
Example, which I see contains a typographic error, here, in case you hadn't found it yet.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid long lines in code tags; the scrolling makes them difficult to read. For reasons explained here, it is probably a bad idea to use tab characters for indentation; use spaces instead. Get a decent text editor like Notepad++ and it can do the indentation for you.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Thanks for the tipp, i use Eclipse and it does it automatically when i hit enter.I guess it uses tabs not sure.
The link yiou sent i just see a tiny lil brown box , kinda microscopic not sure what it is suppose to be,

Mike
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On Eclipse: window->preferences->Java->code style->formatter->indentation->tab policy->spaces only and tab = 4 spaces and indentation = 4 spaces.

the image was of course a life-size photo of my brain.
I presume by now you will have worked out how to avoid those Exceptions with Scanner, but if not, let's see whether this link works any better .
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks once again Campbell, and i am sure your brain is just a tad bit bigger than that ;)

I changed the tab and spaces in Eclipse.. I will also take a look at the link later on.


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