-1
import java.util.*;
public class Javabasics{
    public static void main (String args[])
    {
      Scanner sc=new Scanner(System.in);
      double price=sc.nextDouble();
      System.out.println(price);
}
}

PS D:\JAVA> javac Javabasics.java 
PS D:\JAVA> java Javabasics.java 
12.456
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.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at Javabasics.main(Javabasics.java:9)
PS D:\JAVA> 

The problem is the vs code is not taking the float or double value like 12.1456, it is only taking value of datatype int like 1,23,789,... even after using double or float datatype.

  • Does this answer your question? [Scanner double value - InputMismatchException](https://stackoverflow.com/questions/17150627/scanner-double-value-inputmismatchexception) – maloomeister Nov 09 '22 at 06:44
  • Not related to your problem, but if you compile using javac, then run using `java Javabasics` (without `.java`) **or** skip compiling with `javac Javabasics.java` first. If you use `java Javabasics.java`, you can only run "single source file" programs, and Java will compile it again. – Mark Rotteveel Nov 09 '22 at 12:13

1 Answers1

1

Your title has nothing whatsoever to do with the actual question you then ask.

The problem is locale. Scanners adopt the locale of your platform. On your platform, the locale is configured so that comma, not dot, is the decimal separator. That's why 1,23 does work - that's 1 unit and 23 hundreths.

One easy fix is to call sc.useLocale(Locale.ENGLISH);.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72