Exception in thread “main” java.util.inputmismatchexception & How to solve it
In this tutorial, we will explain the exception in thread “main” java.util.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.
1. The Structure of the 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.
2. 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)
3. How to deal with the exception in thread “main” java.util.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 with its execution.
4. Download the Eclipse Project
This was a tutorial about the exception in thread “main” java.util.inputmismatchexception in Java.
You can download the full source code of this example here: Exception in thread “main” java.util.inputmismatchexception & How to solve it
Last updated on Jan. 10th, 2022
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 »