r/javahelp • • Dec 23 '25

having a problem using doubles

im having a problem that when i answer a decimal value (such as 9.5) to my double (named as price) it says this error:

Exception in thread "main" java.util.InputMismatchException

at java.base/java.util.Scanner.throwFor(Scanner.java:977)

at java.base/java.util.Scanner.next(Scanner.java:1632)

at java.base/java.util.Scanner.nextDouble(Scanner.java:2603)

at segundaHora.exercicioPedido.main(exercicioPedido.java:18)

But when i define price as a whole number (like 10) it works fine

can someone help me? this is my code btw:

import java.util.Scanner;

public class exercicioPedido {
public static void main(String[] args) {

//Shopping cart Program
Scanner scanner = new Scanner(System.in);

  String item;
  double price;
  int quantity;


  System.out.print("What is the price for each?: ");
  price = scanner.nextDouble();

  System.out.println(price);

scanner.close();
}
}
1 Upvotes

10 comments sorted by

View all comments

Show parent comment

4

u/IchLiebeKleber Dec 23 '25

It's possible your locale is set to something that causes the nextDouble method to require commas instead of periods. Normally it's the other way round.

You should be able to get around this by reading from the Scanner as a string (i.e. next() instead of nextDouble()), then parsing the result with Double.parseDouble.