WHILE does not work

0
necesito que si tipo == "LATA", no entre al ciclo, pero igualmente lo hace

private static String ingTipo() {
        System.out.println("Ingrese tipo de envase");
        String tipo = scan.nextLine();
        while (tipo != "LATA") {
            System.out.println("Ingrese tipo de envase");
            tipo = scan.next();
            }
        return tipo;
    }
    
asked by juan 24.09.2017 в 23:11
source

1 answer

1

To compare String , the method equals() is more effective.

private static String ingTipo() {

    System.out.println("Ingrese tipo de envase");
    String tipo = scan.nextLine();

    // Mientras tipo sea diferente a LATA se ejecuta el ciclo
    while (!tipo.equals("LATA")) {
        System.out.println("Ingrese tipo de envase");
        tipo = scan.next();
    }

    return tipo;
}
    
answered by 24.09.2017 в 23:49