[Solved] Exception in thread “main” java.util.InputMismatchException

Exception in thread main java.util.InputMismatchException is an unchecked runtime exception thrown by a scanner class when the retrieved input data type does not match the expected type. In this article, we will study the detailed meaning of this exception, its different causes, and possible solutions. Stay tuned and keep reading this article.

Exception in thread "main" java.util.InputMismatchException

What is java.util.InputMismatchException?

Hierarchy of InputMismatchexception:

java.util.InputMismatchException Hierarchy

The above figure represents the hierarchy of an InputMismatchException. InputMismatchException extends NoSuchElementException, a runtime exception is thrown when the requested element does not exist. InputMismatchException included in java since java version 1.5.

Constructors of InputMismatchException

There are the following two constructors available for InputMismatchException.

public InputMismatchException()

It constructs this exception without an error message. This constructor creates a null error message for the exception.

public InputMismatchException(String s)

It constructs this exception with an error message. You can access the error message using the getMessage() method.

How to solve exception in thread main java.util.InputMismatchException?

Here we will see two scenarios where this error message can arise and how to solve them.

Wrong variable type:

Example:

package com.technolads;
import java.util.Scanner;

public class Technolads_Test {
 
    public static void main(String[] args) 
    {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your name: ");
 
        //Get value from the user and store it
        int userName = scanner.nextInt();
        
        scanner.close();
 
        // Display value provided by the user
        System.out.println("The name provided by the user is: "+userName);
    }
}

When the above java code is executed it will give the following error message

Enter your name: Technolads
Exception in thread "main" java.util.InputMismatchException
              at java.base/java.util.Scanner.throwFor(Scanner.java:939)
              at java.base/java.util.Scanner.next(Scanner.java:1594)
              at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
              at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
              at com.technolads.Technolads_Test.main(Technolads_Test.java:14)

Solution:

In the above example userName is declared as Int and the user-provided value is in the string format. A name cannot be an integer value. Hence change the type of userName variable from Int to String. And also change method nextInt() to next() as shown below

String userName = scanner.next();

Invalid input data

Example:

package com.technolads;
import java.util.Scanner;

public class Technolads_Test {
 
    public static void main(String[] args) 
    {
    
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter your age: ");
 
        //Get value from the user and store it
        int userAge = scanner.nextInt();
        
        scanner.close();
 
        // Display value provided by the user
        System.out.println("Age provided by the user is: "+userAge);
    }
}

When the above code is executed, it will give the following output

Enter your age: 28.5
Exception in thread "main" java.util.InputMismatchException
	at java.base/java.util.Scanner.throwFor(Scanner.java:939)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at com.technolads.Technolads_Test.main(Technolads_Test.java:12)

Solution:

In the above example, the userAge variable is declared as an Int but the user has provided a decimal value. Which results in the exception. This is caused due to invalid input data. User should provide an integer value as an age.

Bonus Tip:

Implement an Error check to handle the exception. In the wrong input data scenario, the user will not able to understand what went wrong just by looking at the above exception error message. Instead of that, you can provide a custom error message for bettor understanding for the user as shown below.

package com.technolads;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Technolads_Test {
 
   

	public static void main(String[] args) 
    {   
		int userAge = 0;
    	try 
    	{
    		Scanner scanner = new Scanner(System.in);
            System.out.print("Enter your age: ");
            
            //Get value from the user and store it
            userAge = scanner.nextInt();
            
            scanner.close();
            // Display value provided by the user
            System.out.println("Age provided by the user is: "+userAge);
    	}
    	catch(InputMismatchException exp)
    	{
    		System.out.println("Error: Input value is invalid. Please provide an integer value: ");
    	}
        
    }
}

Output:

Enter your age: 28.5
Error: Input value is invalid. Please provide an integer value: 

Conclusion:

Here we have learned two scenarios where an Exception in thread main java.util.InputMismatchException can occur and their possible solution. And also we have learned how to handle this exception and provide more information about the error message to the end-user if there is any issue with the input data.

We hope that you are able to get rid of or handle the error message using one of the above methods. If you are still facing the issue then please do mention it in the comment section or you can reach out to us using the Contact Form. You can also contact us on our official mail id hello.technolads@gmail.com

[Solved] unchecked runtime.lasterror: the message port closed before a response was received

[Solved] Date Format Error: java.text.parseexception: unparseable date

Leave a Comment