InputMismatchException in Java
What is InputMismatchException?
One of the most frequent errors in Java is the InputMismatchException. Because the InputMismatchException is a subtype of the java.lang, it is an unchecked exception. RuntimeException.Because it is a subtype of both java.lang.Throwable and java.lang.Object, it offers every method that is offered by those types.
In other words, it is defined that A Scanner object will throw an InputMismatchException runtime exception Java if a token it has retrieved doesn't really match the criteria for the expected type or is beyond the expected type's range.InputMismatchException does not need to be stated in the throws section of a method or function Object() because it is an unchecked exception.
Why Do InputMismatchExceptions Occur?
When utilizing Java programs that employ the Scanner class to prompt users for input, the InputMismatchException frequently occurs. When the data is incorrect for the intended type, an exception may occur. The input is either outside of the acceptable range or does not follow the template for the intended type.
An InputMismatchException is raised, for instance, when a user enters a String value when a program expects an Integer value.
When is an InputMismatchException raised?
The Scanner raises this unchecked exception to show that the returned token does not fit the pattern for the intended type or that it is beyond the expected type's range.
A Scanner is offered by java.utilpackage.a class that accepts user-provided text and rudimentary data types as input. In Java, it is the easiest technique of reading user input. When a user provides an incorrect type of input or input that is outside of the acceptable range, the InputMismatchException occurs.Simply, when the input type is incorrect, we receive the InputMismatchException. The Scanner throws the InputMismatchException when given a float value as input when it is expecting an integer.The Scanner will indeed throw the InputMismatchException if we supply a big integer value as input when the Scanner requires an integer.
How to Resist InputMismatchException?
In Java, avoiding InputMismatchException is fairly easy. There is only one easy way to prevent this exception: give the scanner valid input (either data of a similar type or data that should fall within an expected data type range).
Fixing InputMismatchException
It is important to make sure that inputs for a Scanner object belong to the right type and are acceptable for the expected type in order to prevent the InputMismatchException. If an exception is raised, the input data's format needs to be examined and corrected in order for the program to function properly.
- InputMismatchException offers every method that the java.lang.Throwable and java.lang.Object classes offer because it is a subclass of each of them.
java.lang.Throwable class methods
addSuppressed()
fillInStackTrace()
getCause()
getLocalizedMessage()
getMessage()
getStackTrace()
getSuppressed()
initCause()
printStackTrace()
printStackTrace()
printStackTrace()
setStackTrace()
toString().
Java.lang.Object class methods
clone()
equals()
finalize()
getClass()
hashCode()
notify()
notifyAll()
wait().
Constructors of InputMismatchException?
- Public InputMismatchException()
creates an InputMismatchException with the error string null.
- Public InputMismatchException(String s)
It creates an InputMismatchException and stores a pointer to the error message string s for retrieval by the getMessage function.
Arguments:The detailed message is s.
Examples of InputMismatchException
Example 1
import java.util.Scanner;
public class InputMismatchExceptionExample
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an integer: ");
int integer = scanner.nextInt();
scanner.close();
System.out.println("You entered: " + integer);
}
}
The output of the program
java -cp /tmp/pCdDCZnKjBInputMismatchExceptionExample
Enter an integer:
4
You entered: 4
Example 2
import java.util.Scanner;
public class EmployeeData
{
int salary;
String name;
public EmployeeData(String name, int salary)
{
this.salary = salary;
this.name = name;
}
public void display() {
System.out.println("Name of the Employee is: "+name);
System.out.println("salary of the Employee is: "+salary);
}
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = sc.next();
System.out.println("Enter your salary: ");
int salary = sc.nextInt();
EmployeeDataobj = new EmployeeData(name, salary);
obj.display();
}
}
The output of the program
Enter your salary:
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 EmployeeData.main(EmployeeData.java:21)
Enter your name: sravani
Enter your salary: 20000
Name of the Employee is: avanisalary of the Employee is: 0
Example 3
import java.util.InputMismatchException;
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter the Value: ");
Integer inputInt = sc.nextInt();
System.out.println(inputInt);
}
catch (InputMismatchException ex)
{
System.out.println("We have given input as float expecting integer "+ ex);
}
}
}
The output of the program
Enter the Value:
1.6
We have given input as float expecting integer java.util.InputMismatchException