0

When I use the the .nextDouble() in the Scanner class, I get a InputMismatchException error, despite the input being a floating point number

This is a simple code to show the problem:

import java.util.Scanner;

public class BasicJava1
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter a number");

        double n = input.nextDouble();
    }
}

Output:

Enter a number
5.5
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
    at BasicJava1.main(BasicJava1.java:11)
  • This only happens with floating point numbers, when I enter an integer to this same code, it works
  • This only happens when I use a local Terminal or IDE, I tried it with an online compiler, and it worked fine
  • I use a mac
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    Does this answer your question? [Scanner double value - InputMismatchException](https://stackoverflow.com/questions/17150627/scanner-double-value-inputmismatchexception) – maloomeister Oct 14 '21 at 13:47
  • TL;DR: This is due to your default locale, where the decimal delimiter is a `,` instead of a `.`. So specify a different locale or enter a number with `,` as delimiter. – maloomeister Oct 14 '21 at 13:47

0 Answers0