1. Overview
In this tutorial, We'll learn what is the meaning of java.util.InputMismatchException and how do we solve and handle this error java.util.InputMismatchException.
2. What is the meaning of InputMismatchException?
inputmismatchexception is thrown by the jvm when it is expecting one type of value as input but it received a different type of data. That means value if does not match with the regular expression pattern or out of range.
For example, if the program is expecting to get the integer value as input but it got the string values. So, this is called input mismatch. So, it is called inputmismatchexception in java.
In Java, all the errors are thrown as errors or exceptions.
3. Hierarchy of InputMismatchException class
InputMismatchException class extends NoSuchElementException which means that is requested data is not present.
NoSuchElementException class extends RuntimeException to indicate this is not a compile time error.
Below is the complete hierarchy.
Object <-- Throwable <-- Exception <-- RunTimeException <-- NoSuchElementException <--InputMismatchException
4. InputMismatchException Constructors
InputMismatchException class comes with two constructors.
InputMismatchException(): Creates InputMismatchException object without any error message.
InputMismatchException(String message): Generates InputMismatchException objecgt with the given message.
5. InputMismatchException Example 1
We can generate InputMismatchException using Scanner class. By using Scanner class we can read the input from the user keyboard by using nextInt() method.
nextInt() method expects integer value from user console.
Look at the below program and its output.
package com.javaprogramto.exception.inputmismatchexception; import java.util.Scanner; public class InputMismatchExceptionExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter a number"); int number = scanner.nextInt(); if (number % 2 == 0) { System.out.println(number + " is even"); } else { System.out.println(number + " is odd"); } } }
Output:
Please enter a number number 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.javaprogramto.exception.inputmismatchexception.InputMismatchExceptionExample.main(InputMismatchExceptionExample.java:12)
In this example, an integer value is expected but found alphabets in it. So, it is not able to match numeric regular expression patterns.
6. InputMismatchException Example 2
When you are assigning the BigDecimal values to the array but got the string values.
package com.javaprogramto.exception.inputmismatchexception; import java.math.BigDecimal; import java.util.Scanner; public class InputMismatchExceptionExample2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter BigDecimal array size: "); int size = scanner.nextInt(); BigDecimal[] array = new BigDecimal[size]; for (int i = 0; i < size; i++) { array[i] = scanner.nextBigDecimal(); } } }
Output:
Enter BigDecimal array size: 5 25 qwe 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.nextBigDecimal(Scanner.java:2742) at com.javaprogramto.exception.inputmismatchexception.InputMismatchExceptionExample2.main(InputMismatchExceptionExample2.java:18)
7. How to solve java.util.InputMismatchException?
To solve java.util.InputMismatchException, we need to make sure the validations are in place and passing correct data type values.
Most of the time this comes because of bad data and we need to correct it to fix.
8. Conclusion
In this article, We've seen how to fix InputMismatchException from java util package.
GitHub
No comments:
Post a Comment
Please do not add any spam links in the comments section.