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
  • 43
  • 1
  • 2
  • 5
  • What if you enter `1000,0`? – BackSlash Sep 22 '13 at 13:47
  • 3
    @MarounMaroun I think it depends on the country where you are. I'm in Italy and decimal numbers are written with a comma, not a dot, so if i enter `1000.0` i get an `InputMismatchException`, if i enter `1000,0` everything goes fine. – BackSlash Sep 22 '13 at 13:51
  • yeah I also heard this that in france `,` works as `.` in numbers , @BackSlash with which locale you checked it ? – exexzian Sep 22 '13 at 13:55
  • @sansix I checked with the default locale, which is `Locale.ITALY` for me – BackSlash Sep 22 '13 at 14:00

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
  • 38,481
  • 23
  • 112
  • 156