Why am I Getting Inputmismatchexception

Why am I getting InputMismatchException?

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

try {
// ...
} catch (InputMismatchException e) {
System.out.print(e.getMessage()); //try to find out specific reason.
}

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.

import java.util.*;

public class Test {
public static void main(String... args) {
new Test().askForMarks(5);
}

public void askForMarks(int student) {
double marks[] = new double[student];
int index = 0;
Scanner reader = new Scanner(System.in);
while (index < student) {
System.out.print("Please enter a mark (0..30): ");
marks[index] = (double) checkValueWithin(0, 30);
index++;
}
}

public double checkValueWithin(int min, int max) {
double num;
Scanner reader = new Scanner(System.in);
num = reader.nextDouble();
while (num < min || num > max) {
System.out.print("Invalid. Re-enter number: ");
num = reader.nextDouble();
}

return num;
}
}

As you said, you have tried to enter 1.0, 2.8 and etc. Please try with this code.

Note : Please enter number one by one, on separate lines. I mean, enter 2.7, press enter and then enter second number (e.g. 6.7).

why is it giving me an java.util.InputMismatchException when i run this code when I use a text file with the text (shown underneath)

You should change the delimiter to include optional spaces around commas, and the new line characters as well. Something like this:

scanner.useDelimiter("(\\s*,\\s*)|(\r\n)|(\n)");

Why am I getting inputMismatchException even when using hasNextInt() method?

The problem is in the logic inside the while. In the commented testID() method, you checked the following condition to be true:

while(!input.hasNextInt()) {
....
}

Thus for a non integer input, input.hasNextInt() will return false and !hasNextInput() will consequently return true and the while will keep on looping till a valid integer was entered.

Now in case 2, the condition inside the while is always false,

while(!input.hasNextInt()) && (0 > studentID) && (studentID > 999999) {
...
}

See, here studentID is set by default to -1 thus even though !input.hasNextInt() returned true as expected, the result of anding this true with (studentID > 999999) is false. So the code will never go into the while loop and move on to the next line which happens to be,

studentID = input.nextInt();

This will throw a InputMismatchException since the value entered was non an integer

Getting InputMismatchException when trying to enter a string with whitespaces

This happens because the default delimiter of a token is a white space. You just have to set some configuration when init the Scanner object:

Scanner input = new Scanner(System.in).useDelimiter("\\n");

Input Mismatch Exception

Your system's locale is not supporting dot as decimal separator. Use a locale which supports dot as a separator e.g. Locale.ENGLISH.

Scanner radius = new Scanner(System.in).useLocale(Locale.ENGLISH);

A sample run:

Please input the radius of circle: 7.5
Length = 47.12388980384689
Area = 176.71458676442586


Related Topics



Leave a reply



Submit