How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)?

You can use one of the many hasNext methods that Scanner has for pre-validation.

You can use one of the many hasNext* methods that Scanner has for pre-validation. If (in.hasNextInt()) { int a = in.nextInt() ; System.out. Println(a); } else { System.out.

Println("Sorry, couldn't understand you! "); } This prevents InputMismatchException from even being thrown, because you always make sure that it WILL match before you read it. Java.util.

Scanner API boolean hasNextInt(): Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. The scanner does not advance past any input. String nextLine(): Advances this scanner past the current line and returns the input that was skipped.Do keep in mind the sections in bold.

HasNextInt() doesn't advance past any input. If it returns true, you can advance the scanner by calling nextInt(), which will not throw an InputMismatchException. If it returns false, then you need to skip past the "garbage".

The easiest way to do this is just by calling nextLine(), probably twice but at least once. Why you may need to do nextLine() twice is the following: suppose this is the input entered: 42enter too many! Enter 0enter Let's say the scanner is at the beginning of that input.

HasNextInt() is true, nextInt() returns 42; scanner is now at just before the first enter. HasNextInt() is false, nextLine() returns an empty string, a second nextLine() returns "too many!"; scanner is now at just after the second enter. HasNextInt() is true, nextInt() returns 0; scanner is now at just before the third enter.

Here's an example of putting some of these things together. You can experiment with it to study how Scanner works. Scanner in = new Scanner (System.

In) ; System.out. Println("Age? "); while (!in.hasNextInt()) { in.next(); // What happens if you use nextLine() instead?

} int age = in.nextInt(); in.nextLine(); // What happens if you remove this statement?System.out. Println("Name? "); String name = in.nextLine(); System.out.

Format("%s is %d years old", name, age); Let's say the input is: He is probably close to 100 now...enter Elvis, of courseenter Then the last line of the output is: Elvis, of course is 100 years old.

This looks prommising. Could you please explain explicitly what hasNextInt() does? – David Mar 23 '10 at 1:27.

In general I really, really dislike using the same library call for both reading and parsing. Language libraries seem to be very inflexible and often just can't be bent to your will. The first step that pulls data from System.In should not be able to fail, so have it read it as a string into a variable, then convert that string variable to an int.

If the conversion fails, great--print your error and continue. When you wrap your stream with something that can throw an exception, it gets kind of confusing just what state the whole mess leaves your stream in.

It's always a benefit to have your application throw an error when an error occurs opposed to ways to keep it from happening. One alternative is to wrap the code inside a try {...} catch {...} block for InputMismatchException. You might also want to wrap the code inside a while loop to have the Scanner keep prompting until a specific condition is met.

You should move "found=true;" to the top of try statement. Now it is always true – Oskar Kjellin Mar 22 '10 at 23:21 I omitted my example, it didn't look right to me. I just wanted to give the main points: you should catch errors that are being thrown, not work around them and a technique to "re-prompt".

Thanks anyway Kurresmack. – Anthony Forloney Mar 22 '10 at 23:26 how exactly does a try catch block work? I'v never used one before.

– David Mar 22 '10 at 23:54 I am on my phone, ill try my best. Try { // code here } catch(InputMismatchException e) { // catch wrong format } – Anthony Forloney Mar 22 '10 at 23:59 that code looks interesting you should re post it if you can. – David Mar 22 '107 at 1:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions