10

So far I have this:

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;
}

and this:

public void askForMarks() {
    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++;
    }
}

When I test this, it can't take double number and I got this message:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at MarkingSystem.checkValueWithin(MarkingSystem.java:25)
at MarkingSystem.askForMarks(MarkingSystem.java:44)
at World.main(World.java:6)
Java Result: 1

How do I fix this?

Ravi
  • 30,829
  • 42
  • 119
  • 173
Trần Trung
  • 171
  • 1
  • 2
  • 9
  • http://docs.oracle.com/javase/1.5.0/docs/api/java/util/InputMismatchException.html Look at this. Maybe the scanner was not able to parse what you entered into the console? For example, it asked for a number, you entered "hello"? – Name Dec 25 '12 at 04:00
  • Try doing "nextFloat" instead. – Name Dec 25 '12 at 04:02
  • Remove Scanner reader = new Scanner(System.in); from the askForMarks(); Everything works for me then. – Joe Dec 25 '12 at 04:02

5 Answers5

11

Instead of using a dot, like: 1.2, try to input like this: 1,2.

linha9
  • 121
  • 1
  • 2
9

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).

Jonathan Rosenne
  • 2,159
  • 17
  • 27
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Like I said, I only type in double value such as 1.0, 5.8, etc and that still displays the same message – Trần Trung Dec 25 '12 at 04:52
  • @TrầnTrung copy whole code and compile and run it. And, try to give some input, which you have tried earlier. Also, please check my **Note** at the last of my answer. And, let me know.If you got the solution, then also let me know. – Ravi Dec 25 '12 at 07:01
  • 1
    I think I found the problem, because my computer only recognize the ',' as for the fractional part while the inputs I used are with '.', this is why it wouldn't work. – Trần Trung Dec 25 '12 at 07:15
3

Since you have the manual user input loop, after the scanner has read your first input it will pass the carriage/return into the next line which will also be read; of course, that is not what you wanted.

You can try this

try {
    // ...
} catch (InputMismatchException e) {
    reader.next(); 
}

or alternatively, you can consume that carriage return before reading your next double input by calling

reader.next()

Infinity
  • 3,695
  • 2
  • 27
  • 35
3

I encountered the same problem. Strange, but the reason was that the object Scanner interprets fractions depending on localization of system. If the current localization uses a comma to separate parts of the fractions, the fraction with the dot will turn into type String. Hence the error ...

0

Are you providing write input to the console ?

Scanner reader = new Scanner(System.in);
num = reader.nextDouble();  

This is return double if you just enter number like 456. In case you enter a string or character instead,it will throw java.util.InputMismatchException when it tries to do num = reader.nextDouble() .