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