-1

I get an exception in my code, so it doesnt show more than the first print request, te problem seems to start at line 12 i dont know if i need something else, or is a problem with the int variable type

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 App.main(App.java:12)```
// En un supermercado se trabajan 5 días a la semana. EupJava. Que lea la cantidad de empleados que trabajan por día y las horas que trabajo cada empleado por día. Imprimir:
//- Cuantos empleados trabajaron el día 1
//- Numero de empleado que trabajo más horas, cuantas horas fueron y en que día fue
//- Total de horas trabajadas el día 5
import java.util.*;
public class App {
    public static void main(String[] args) throws Exception {
        Scanner leer = new Scanner (System.in);
        int i, empleados, h, e1=0, mhora=0, hrs, nempleado=0, dia = 0, dia5 = 0;
        for(i = 1; 1 <= 5; i++) {
    System.out.println("Pon el numero de empleados por dia" + i);
    empleados = leer.nextInt();
    if(i == 1) {
        e1 = empleados;
    }

    for(h = 1; h < empleados; h++) {
        System.out.println("Pon el numero de horas");
        hrs = leer.nextInt();
        if (hrs > mhora) {
            mhora = hrs;
            dia = i;
            dia = i;
        }
        if (i == 5){
            dia5 = dia5 + hrs;
        }
    }
    System.out.println("La cantidad de empleados que trabajo el día 1 es de"+e1);
    System.out.println("El empleado que trabajo más horas fue el"+nempleado+"trabajo"+mhora+"y fue en el día"+dia);
    System.out.println("El total de horas rabajadas en el día 5 fue de"+ dia5);
        }       

    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    From the _javadoc_ of class `java.util.InputMismatchException`: _Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type._ You are not entering a valid integer when this line of your code executes: `empleados = leer.nextInt();` Are you entering a very big number that is larger than `Integer.MAX_VALUE`? – Abra Nov 19 '21 at 03:39

1 Answers1

1

From what I can tell, it seems like you might be inputting characters instead of numbers.

Specifically at this stacktrace:

-----------------------------------------------------------
     means that it doesn't match a specified pattern
         possibly int>String doesn't work?
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)
        ----------------------------------------------------------
                           Exception in nextInt
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        ----------------------------------------------------------
        at App.main(App.java:12)

sources
InputMismatchException -- https://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html

If you mean by the locale, if you're inputting 1.2 or such, try changing the locale by using .useLocale(Locale.ENGLISH), as it could be interpreting something that's not like 1,2 as a string.

sources
locale -- Why am I getting InputMismatchException?

acaiberii
  • 48
  • 1
  • 7