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

About Sotirios-Efstathios Maneas

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

Constructors

  • 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:

InputMismatchException.java:

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));
		
		while(scanner.hasNext())
			System.out.println(scanner.nextInt());
		
		//Close the scanner.
		scanner.close();
	}
}

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:

100
50
30
1.1
200
10

then, the execution of our application is shown below:

100
50
30
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.

Download
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

 

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz