Home » Java Basics » exceptions » java.util.InputMismatchException – How to solve InputMismatchException

About Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.

java.util.InputMismatchException – How to solve InputMismatchException

In this tutorial we will discuss about the InputMismatchException in Java. This exception is thrown by an instance of the Scanner class to indicate that a retrieved token does not match the pattern for the expected type, or that the retrieved token is out of range.

The InputMismatchException class extends the NoSuchElementException class, which is used to indicate that the element being requested does not exist. Furthermore, the NoSuchElementException class extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception and thus, it does not need to be declared in a method’s or a constructor’s throws clause.

Finally the InputMismatchException class exists since the 1.5 version of Java.

The Structure of InputMismatchException


  • InputMismatchException()
  • Creates an instance of the InputMismatchException class, setting null as its message.

  • InputMismatchException(String s)
  • Creates an instance of the InputMismatchException class, using the specified string as message. The string argument indicates the name of the class that threw the error.

The InputMismatchException in Java

As we have already described, the InputMismatchException class indicates that a retrieved token does not match a pattern. For example, an application expects to read integers from an input file, but instead, a real number is read. In this case, we have an input mismatch and thus, an InputMismatchException will be thrown:


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class InputMismatchExceptionExample {
	//The name of the input file.
	private final static String filename = "input.txt";
	public static void main(String[] args) throws FileNotFoundException {
		Scanner scanner = new Scanner(new File(filename));
		//Close the scanner.

In this example, we read sample integer values from an input file. If the requested value is not an integer, this an InputMismatchException will be thrown. For example, if the file input.txt contains the following values:


then, the execution of our application is shown below:

Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:864)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at main.java.InputMismatchExceptionExample.main(InputMismatchExceptionExample.java:15)

How to deal with the InputMismatchException

In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.

Download the Eclipse Project

This was a tutorial about the InputMismatchException in Java.

You can download the full source code of this example here: InputMismatchExceptionExample.zip.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Wen Cabrel
Wen Cabrel
1 year ago

Hi, I’m using eclipse oxygen for win32 well I wrote a basic program to calculate the area of a circle, when I input an integer value, it doesn’t have any problem but when I input a floating number it shows me that error.it has been three days trying to solve it but I can’t please does anyone can give me a help? This is my code package test0; import java.util.Scanner; public class RadiusArea { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); //ask the user to enter a Radius System.out.print(“Enter a number… Read more »