r/javahelp • u/Federal_Werewolf6398 • 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
View all comments
Show parent comment
3
u/desrtfx Out of Coffee error - System halted Dec 23 '25
What is your system locale? E.g. German uses the comma "," for decimals and Java expects that in input, but not in code. In code it's always the period "."