The input is this:
185.50
250.36
163.45
535.20
950.22
450.38
-1
it throws me this:
Exception in thread "main" java.util.InputMismatchException<br>
at java.base/java.util.Scanner.throwFor(Scanner.java:939)<br>
at java.base/java.util.Scanner.next(Scanner.java:1594)<br>
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)<br>
at Pruebas.Ventas.main(Ventas.java:25)
but if I input this:
185,50
250,36
163,45
535,20
950,22
450,38
-1
It works perfectly! why?
import java.util.Scanner;
public class Ventas {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double numero = 0;
double grande = 0;
double pequeno = 0;
double suma = 0;
double media = 0;
String comprobacion = "";
int grandePos = 0;
int pequenoPos = 0;
String[] semana = {"Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo"};
while(numero != -1) {
double[] dias = new double[6];
for (int i = 0; i < dias.length; i++) {
numero = scan.nextDouble(); // This is line 25!
suma += numero;
if(numero != -1) {
dias[i] = numero;
if(numero > grande) {
grande = numero;
grandePos = i;
}else if(pequeno == 0 || numero < pequeno) {
pequeno = numero;
pequenoPos = i;
}
}else {
break;
}
}
media = ((suma - dias[5]) / 5);
if(media < dias[5]) {
comprobacion = "NO";
}else {
comprobacion = "SI";
}
}
System.out.println(semana[grandePos] + " " + semana[pequenoPos] + " " + comprobacion);
scan.close();
}
}
Your default locale uses the comma character (,
) as the decimal separator, and thus fails parsing inputs with decimal points (.
) as doubles. You can override this behavior by explicitly setting a Locale
that accepts decimal points. E.g.:
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.US);