0

My code:

public class JavaApplication3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner keyboard = new Scanner(System.in);

        System.out.println( "What city is the capital of France?" );
        keyboard.next();

        System.out.println( "What is 6 multiplied by 7?" );
        keyboard.nextInt();

        System.out.println( "Enter a number between 0.0 and 1.0." );
        keyboard.nextDouble();

        System.out.println( "Is there anything else you would like to say?" );
        keyboard.next();
    }

}

i run normal before a question Enter a number between 0.0 and 1.0. and it's error like this :

***What city is the capital of France?

Paris

What is 6 multiplied by 7?

442

Enter a number between 0.0 and 1.0.

0.5

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.nextDouble(Scanner.java:2413)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:28)
C:\Users\Administrator\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 7 seconds)***
KarelG
  • 5,176
  • 4
  • 33
  • 49
Space
  • 13
  • 3

1 Answers1

3

Scanner will throw this exception if the entry is in a format that is incorrect for the Scanner's Locale. Particularly, in your case, if the wrong decimal separator is used. Both . and , are common locale-specific decimal separators.

To find out what the decimal separator is for your default locale you may use:

System.out.println(
    java.text.DecimalFormatSymbols.getInstance().getDecimalSeparator()
);

See more in "original" topic here.

Community
  • 1
  • 1
Peter R
  • 384
  • 1
  • 11