Andy G Andy G - 1 month ago 4
Java Question

How to use Try/Catch to prevent String input where an Int is needed

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package freetime;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
*
* @author Andy
*/
public class NewClass {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int userInput=0,notInt=0,newInput=0;
System.out.println("Enter a integer number");
while (notInt == 0){
try {
newInput=scan.nextInt();
userInput=newInput;
if (userInput != newInput){
notInt=0;
}
if (userInput == newInput){
notInt=1;
}
}
catch(InputMismatchException e){
System.out.println("That is not an integer, please try again." );
}
}
System.out.println(userInput);

}
}


I am trying to prevent a string input and allow the user to re input as an int. i cannot seem to get it to work properly, i also did this without the while loop. I think my understanding of how the Try Mismatch functions work is causing some issues.

Thanks for the help.

Scanner scan=new Scanner(System.in);
int userInput=0,notInt=0,newInput=0;
boolean runL=true;
while (runL){
System.out.println("Enter a integer number");
if (scan.hasNextInt()){
userInput=scan.nextInt();
runL=false;
}
else {
System.out.println("That is not an integer, please try again.");
}
}
System.out.println(userInput);


So i changed the code to this but I am still running into the loop continuing to go after something other than an int is entered. What am i doing wrong?

int userInput=0,notInt=0,newInput=0;
boolean runL=true;
while (runL){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a integer number");
if (scan.hasNextInt()){
userInput=scan.nextInt();
runL=false;
}
else {
System.out.println("That is not an integer, please try again.");
}
}
System.out.println(userInput);


I've found the problem, I needed to create the scanner object inside of the While loop. Thanks for the help!

Answer

I think my understanding of how the Try Mismatch functions work is causing some issues.

You are probably right there ...

When I remove some of the "strange" code from your try / catch statement it looks like this:

    try {
        newInput = scan.nextInt();
        // do stuff
    } catch (InputMismatchException e){
        // do other stuff
    }

Here's what that actually means:

  1. Call scan.nextInt()

  2. If the call succeeds (i.e. it doesn't throw an exception), then:

    1. assign the result to newInput

    2. "do stuff"

  3. If the call failed, AND it threw a InputMismatchException, then:

    1. "do other stuff"

So, based on that, you don't need to do anything in "do stuff" to test if you just got a valid input. You know that is the case.

And likewise, any stuff that needs to be do in the case where you didn't get a valid input should be in "do other stuff".


Having said that, Jon Skeet's suggestion to look at the hashNextInt method is spot on. If you use that the right way, you won't get an exception at all. The code will be much simpler.

And Jon is also correct that you should be using the boolean type for your true / false logic. (That is what the type is intended for!)