InputMismatchException in Java with Examples

In this tutorial, we will discuss the InputMismatchException in java. Also, we will answer whether InputMismatchException is a checked exception or unchecked exception. Before diving deep into the topic let us first understand when does InputMismatchException occur.

Read Also:  java.lang.NumberFormatException: For input string

When does InputMismatchException occur?

According to Java docs, 

InputMismatchException is thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

In simple words,

The java.util package provides a Scanner class to take input of primitive data types and strings. It is the simplest way to read user input in java. If the user does not provide the proper type of input or input is out of range, then InputMismatchException happens.

InputMismatchException is an Unchecked Exception


The java.util package provides InputMismatchException class that inherits NoSuchElementException class. NoSuchElementException inherits RuntimeException. So, InputMismatchException is a runtime exception. Hence, it is an unchecked exception.

This class has two constructors that are listed below.

public InputMismatchException() 
It is not accepting any exception message parameter. By default, it gives null value as message.

public InputMismatchException(String s)
This constructor receives exception message as parameter.

Example of InputMismatchException

The InputMismatchException exception is produced only when the input type is not proper, for example, if java application expects long datatype as input but the user gives the float value as input it should generate InputMismatchException exception. Please find below the code example.

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

public class JavaHungry {
    
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
         try {
            System.out.println("Enter Integer Value: ");
            Integer inputInt = scanner.nextInt(); // input : "1.1"  
            System.out.println(inputInt);

         } 
         catch (InputMismatchException ex) {
            System.out.println("We have given input as float expecting integer "+ ex);
         }
   }
}

Output:
Enter Integer Value: 1.1
We have given input as float expecting integer java.util.InputMismatchException


In the above example, if the application is looking for integer value but we have provided a float value.

Case 2: Again if we provide an input that is not within integer range

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

public class JavaHungry {
    
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
         try {
            System.out.println("Enter Integer Value: ");
            Integer inputInt = scanner.nextInt(); // input : "12222222222222222"  
            System.out.println(inputInt);

         } 
         catch (InputMismatchException ex) {
            System.out.println("input is not with in integer range "+ ex);
         }
   }
}

Output:
Enter Integer Value: 12222222222222222
input is not with in integer range java.util.InputMismatchException


These examples are true for every datatype. If input data does not comply below two conditions, then InputMismatchException will generate.

1.    Input data type not match.
2.    Input data is not in the range of expected datatype.

In the above examples, we are exiting from the code when the exception generates.

How to avoid InputMismatchException

When we provide a valid input that will comply with the conditions described in the above section. Only correct input can avoid this kind of exception.

That's all for today, please mention in comments in case you have any questions related to InputMismatchException in java.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry