0

I'm learning Java and I got some problems when I try input double floating numbers.

Like this.

import java.util.Scanner;

class Program {
     public static void main (String[] args) {
           Scanner inp = new Scanner(System.in);
           double number = inp.nextDouble();
           System.out.println(number);
     }
}

If I enter 1000 my output will be 1000.0. But if I enter 1000.0, I got this error:

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 test.Test.main(Test.java:6)
Java Result: 1

How could I solve this?

Alvadorn
  • 33
  • 1
  • 2
  • 5

1 Answers1

1

It's likely due to Locale differences between 1000.0 and 1000,0... so you could try this:

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

I hope this helps.

vikingsteve
  • 36,466
  • 22
  • 106
  • 148