• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Catching Exceptions question/problem

 
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
 
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
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
 
Marshal
Posts: 69806
277
  • 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: 69806
277
  • 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: 69806
277
  • 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: 69806
277
  • 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
 
See where your hand is? Not there. It's next to this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic