Search code examples
javadoubledelimiter

How to specify decimal delimiter for Scanner.nextDouble()


I have a fairly simple problem. I am getting real-number input like6.03, but that gives me errors. If I change it to 6,03, it's ok. I, however, can't change the input I need to process, so how do I tell Java to use . as the delimiter instead of ,?

Scanner sc = new Scanner(System.in);
double gX = sc.nextDouble(); // Getting errors

Thanks


Solution

  • Scanner can be provided with Locale to use, you need to specify Locale that uses . as decimal separator:

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