How to fix Input Mismatch Exception in Java real quick!
Stuck with Java.Util.InputMismatchException ? Getting Input Mismatch Exception java error ?. Is your java program failing with "Exception in thread main java.util.InputMismatchException" ?. Here is the detailed article about how to fix InputMismatchException java
What is InputMismatchException in Java?
InputMismatchException is a type of runtime exception in Java. It occurs when the program attempts to read input data that does not match the expected type or format. The InputMismatchException is part of the java.util.Scanner class, which is used to parse input data.
Causes of InputMismatchException:
The most common cause of InputMismatchException is when the program attempts to read input data in a different format than the expected input. For instance, if the program is expecting an integer, but the input is a string, it will throw an InputMismatchException.
Another cause of InputMismatchException is when the program tries to read input data that is out of the range of the expected value. For instance, if the program is expecting an integer between 1 and 10, but the input is 11, it will throw an InputMismatchException.
Here are the step by step procedure to reproduce the issue and fix the InputMismatchException error.
Step By Step Procedure to Reproduce - Input Mismatch Exception Java error.
Input Mismatch Exception java error 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 terms, Input Mismatch Exception occurs when entered value is of wrong type. Example, instead of int, if string value is entered, Scanner class will throw
InputMismatchException.
InputMismatchException is runtime unchecked exception and is a sub class of NoSuchElementException.
Please refer the java class Class InputMismatchException
Example :
Below code would throw InputMismatchException when String value is entered instead of Integer
import java.util.Scanner; public class InputScanner { private static Scanner scanInput; public static void main(String[] args) { // Read an Integer from the console and print try { scanInput = new Scanner(System.in); System.out.println("Input Is " + scanInput.nextInt()); } catch (Exception e) { System.out.println("Something went wrong!!!"); e.printStackTrace(); } } } ----------------------------------------------- Test Run 1 - Enter a string value "Hello" ----------------------------------------------- Input value: Hello Output: Something went wrong!!! 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 InputScanner.main(InputScanner.java:12) ----------------------------------------------- Test Run 2 - Enter a Int value 22 ----------------------------------------------- Input value: 22 Output: Input Is 22
Download the Source Code from Github
# Solutions to InputMismatchException:
- Make sure you are entering the correct value with the right input type. For the above example, instead of int, if string value is provided program would fail
- Use Try-catch Blocks: The best way to handle InputMismatchException is by using a try-catch block. This will allow the program to catch the exception and handle it appropriately.
- Use hasNext() Method: The hasNext() method is used to check if there is another input available. This method can be used to check the input before attempting to read it.
- Use the nextLine() Method: The nextLine() method is used to read the entire line of input, regardless of the type of input. This method can be used to read input data and convert it to the required format.
Prevention Techniques:
Use Regular Expressions: Regular expressions can be used to ensure that input data matches the expected format. For instance, if the program is expecting an email address, a regular expression can be used to ensure that the input is in the correct format.
Validate User Input: Validating user input can help prevent InputMismatchException. This can be done by checking that the input is within the expected range or format.
Conclusion:
InputMismatchException is a common error in Java that occurs when the program attempts to read input data that does not match the expected type or format. The causes of InputMismatchException are varied, but the most common are incorrect input format or out-of-range values.
However, there are solutions and prevention techniques that can be used to handle this error.
By using try-catch blocks, hasNext() and nextLine() methods, regular expressions, and validating user input, Java programmers can prevent and handle InputMismatchException effectively.
Read Next :
- Java is not recognized
- IntellijIdea Installation guide
- Java Reserved vs Contextual Keywords
- Java ERROR code 1603
- Procyon Java Decompiler
- Resultset has now rows. Quick Fix!
- Java Update on Mac
- Java Update check fails
- Java wont install on windows 10
- Java installation error code 80
- Windows does not recognize java
- Access Adobe programs Java
- Failed to install java update
- System breaks jdk8 javac command
- Java Uninstall Stops at Progress Bar
- Could not find java dll
- Eclipse Error code 13
Archives
- Java is not recognized
- IntellijIdea Installation guide
- Java Reserved vs Contextual Keywords
- Java ERROR code 1603
- Procyon Java Decompiler
- Resultset has now rows. Quick Fix!
- Java Update on Mac
- Java Update check fails
- Java wont install on windows 10
- Java installation error code 80
- Windows does not recognize java
- Access Adobe programs Java
- Failed to install java update
- System breaks jdk8 javac command
- Java Uninstall Stops at Progress Bar
- Could not find java dll
- Eclipse Error code 13
- Postgresql driver known issues
- Error Connecting Postgresql
- Multithreading in Java
- Java Collection Code Challenge