Inputmismatchexception java что это
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
Class InputMismatchException
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2022, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
InputMismatchException in Java with Examples
In this tutorial, we will discuss the InputMismatchException in java. Also, we will answer whether InputMismatchException is a checked exception or unchecked exception. Before diving deep into the topic let us first understand when does InputMismatchException occur.
When does InputMismatchException occur?
InputMismatchException is thrown by a Scanner to 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 words,
The java.util package provides a Scanner class to take input of primitive data types and strings. It is the simplest way to read user input in java. If the user does not provide the proper type of input or input is out of range, then InputMismatchException happens.
InputMismatchException is an Unchecked Exception
The java.util package provides InputMismatchException class that inherits NoSuchElementException class. NoSuchElementException inherits RuntimeException. So, InputMismatchException is a runtime exception. Hence, it is an unchecked exception.
This class has two constructors that are listed below.
Example of InputMismatchException
The InputMismatchException exception is produced only when the input type is not proper, for example, if java application expects long datatype as input but the user gives the float value as input it should generate InputMismatchException exception. Please find below the code example.
Output:
Enter Integer Value: 1.1
We have given input as float expecting integer java.util.InputMismatchException
In the above example, if the application is looking for integer value but we have provided a float value.
Case 2: Again if we provide an input that is not within integer range
Output:
Enter Integer Value: 12222222222222222
input is not with in integer range java.util.InputMismatchException
These examples are true for every datatype. If input data does not comply below two conditions, then InputMismatchException will generate.
1. Input data type not match.
2. Input data is not in the range of expected datatype.
In the above examples, we are exiting from the code when the exception generates.
How to avoid InputMismatchException
When we provide a valid input that will comply with the conditions described in the above section. Only correct input can avoid this kind of exception.
That’s all for today, please mention in comments in case you have any questions related to InputMismatchException in java.
Why am I getting InputMismatchException?
When I test this, it can’t take double number and I got this message:
How do I fix this?
5 Answers 5
Instead of using a dot, like: 1.2, try to input like this: 1,2.
Here you can see the nature of Scanner:
double nextDouble()
Returns the next token as a double. If the next token is not a float or is out of range, InputMismatchException is thrown.
Try to catch the exception
UPDATE
CASE 1
I tried your code and there is nothing wrong with it. Your are getting that error because you must have entered String value. When I entered a numeric value, it runs without any errors. But once I entered String it throw the same Exception which you have mentioned in your question.
CASE 2
You have entered something, which is out of range as I have mentioned above.
I’m really wondering what you could have tried to enter. In my system, it is running perfectly without changing a single line of code. Just copy as it is and try to compile and run it.
As you said, you have tried to enter 1.0 , 2.8 and etc. Please try with this code.