2

I am having trouble with entering non-integers into an integer field. I am only taking precautions so that if another person uses/works on my program they don't get this InputMismatchException.

When I enter a non-digit character into the input variable, I get the above error. Is there any way to compensate for this like one could do for a NullPointerException when it comes to strings?

This code is redacted just to include the relevant portions causing the problem.

import java.util.Scanner;

class MyWorld {

public static void main(String[] args) {

   Scanner user_input = new Scanner(System.in);

   int input = 0;   

   System.out.println("What is your age? : ");
   input = user_input.nextInt();
   System.out.println("You are: " +input+ " years old");

  }

}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Yiuri
  • 31
  • 1
  • 4
  • 2
    http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextInt%28%29 – JB Nizet Aug 27 '15 at 21:11
  • To expand on JB Nizet's comment a bit: When working with Java, the documentation is your friend. Always look there first. The [documentation for Scanner](http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html) can tell you a lot about how to use it. – VGR Aug 27 '15 at 21:17
  • @Yiuri what do you really want? You want to prevent an inputmismatchException from occurring is it? – user3437460 Aug 27 '15 at 21:19
  • Thx for your help everyone! Used the user_input.hasNextInt() method. with the if else statement provided by dizzicode and others. Works to perfection now. – Yiuri Aug 27 '15 at 21:55

5 Answers5

2

You can use an if statement to check if user_input hasNextInt(). If the input is an integer, then set input equal to user_input.nextInt(). Otherwise, display a message stating that the input is invalid. This should prevent exceptions.

System.out.println("What is your age? : ");
if(user_input.hasNextInt()) {
    input = user_input.nextInt();
}
else {
    System.out.println("That is not an integer.");
}

Here is some more information about hasNextInt() from Javadocs.

On a side note, variable names in Java should follow the lowerMixedCase convention. For example, user_input should be changed to userInput.

deezy
  • 1,480
  • 1
  • 11
  • 22
0

This is a try-catch block. You need to use this if you want to be sure of not making the program-flow stop.

try { 
  input = user_input.nextInt();
}
catch (InputMismatchException exception) { //here you can catch that exception, so program will not stop
  System.out.println("Integers only, please."); //this is a comment
  scanner.nextLine(); //gives a possibility to try giving an input again
}
Leah
  • 458
  • 3
  • 15
0

You can add a try-catch block:

import java.util.Scanner;

class MyWorld {

public static void main(String[] args) {

   Scanner user_input = new Scanner(System.in);

   int input = 0;   

   System.out.println("What is your age? : ");

   try{
       input = user_input.nextInt();
   }catch(InputMisMatchException ex)
       System.out.println("An error ocurred");
   }

   System.out.println("You are: " +input+ " years old");

  }
}

If you want to provide the user to enter another int you can create a boolean variable and make a do-while loop to repeat it. As follows:

boolean end = false;

//code

do
{
   try{
       input = user_input.nextInt();
       end = true;
   }catch(InputMisMatchException ex)
       System.out.println("An error ocurred");
       end = false;
       System.out.println("Try again");
       input.nextLine();
   }
}while(end == false);
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
0

Test using hasNextInt().

Scanner user_input = new Scanner(System.in);
System.out.println("What is your age?");
if (user_input.hasNextInt()) {
    int input = user_input.nextInt();
    System.out.println("You are " + input + " years old");
} else {
    System.out.println("You are a baby");
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
-1

Use Scanner's next() method to get data instead of using nextInt(). Then parse it to integer using int input = Integer.parseInt(inputString); parseInt() method throws NumberFormatException if it is not int, which you can handle accordingly.

Raman Shrivastava
  • 2,923
  • 15
  • 26