How to get out of a loop with a counter in Java?

1

I have made a small program in which a password must be entered and if it reaches a maximum of three failed attempts, the program ends without further ado. My problem is that with my current code I can not get anywhere, the loop repeats infinitely. Thanks in advance for the help, I have been programming for a short time at JAVA and I do not know how to proceed.

import javax.swing.*;
public class prueba {

public static void main (String[]args) {

    String clave="Juan";
    String pass="";
    int conteo=0;

    while ((clave.equals(pass)==false)||(conteo==3)) {

        pass=JOptionPane.showInputDialog("Introduce la contraseña");

    if (clave.equals(pass)==false) {
        System.out.println("Contraseña Incorrecta");
        conteo++;
    if (conteo==3) {
        System.out.println("EQUIPO BLOQUEADO");
    }
    }


    }
    System.out.println("CONTRASEÑA CORRECTA");
}
}
    
asked by VRivers5 14.12.2018 в 20:37
source

2 answers

0

It is too important to review the following theory:

First you must know that this:

clave.equals(pass)==false

you can reduce it to:

!clave.equals(pass)

since the equals() method is actually performing an evaluation, if clave equals pass . If we add ! indicates that we are denying it, therefore it means that clave is NOT equal to pass .

Now the main problem by which an infinite loop is made is that when starting in reality always clave will be different to pass

while ((!clave.equals(pass))||(conteo==3)) {

What you should do is evaluate the value of pass before starting the loop, if the password is incorrect, ask again for the value to the user:

 pass=JOptionPane.showInputDialog("Introduce la contraseña");

    while ((!clave.equals(pass))||(conteo==3)) {        

      if (!clave.equals(pass)) {
        System.out.println("Contraseña Incorrecta");
        pass=JOptionPane.showInputDialog("Introduce la contraseña");        
        conteo++;
      }

    }    

if the value of clave and pass or ( || ) equals the value of conteo equals 3, the loop will end.

Now here you can evaluate if the password was correct or simply blocked the computer:

if (clave.equals(pass)) { 
    System.out.println("CONTRASEÑA CORRECTA");         
}else{
    System.out.println("EQUIPO BLOQUEADO");
}

Analyze the code:

import javax.swing.*;
public class prueba {

public static void main (String[]args) {

    String clave="Juan";
    String pass="";
    int conteo=0;

    pass=JOptionPane.showInputDialog("Introduce la contraseña");

    while ((!clave.equals(pass))||(conteo==3)) {        

      if (!clave.equals(pass)) {
        System.out.println("Contraseña Incorrecta");
        pass=JOptionPane.showInputDialog("Introduce la contraseña");        
        conteo++;
      }

    }    

    //Después de terminarse el bucle, revisa...
    if (clave.equals(pass)) { //Si la contraseña es correcta.
        System.out.println("CONTRASEÑA CORRECTA");         
    }else{//Nunca fue correcta, bloquea el equipo
        System.out.println("EQUIPO BLOQUEADO");
    }

}
    
answered by 14.12.2018 в 21:30
0

The response of @JorgeSys (sorry not to link users even xD) is very complete. To reduce lines you can also use the break statement to break the loop and use do {} while. It is also better to save success in a boolean and not have to be comparing String unnecessarily.

public static void main(final String[] args) {
    final String clave = "Juan";
    String pass = "";
    int conteo = 0;

    boolean exito = false;
    do {
        pass = JOptionPane.showInputDialog("Introduce la contraseña");
        if (clave.equals(pass)) {
            exito = true;
        } else {
            System.out.println("Contraseña Incorrecta");
            conteo++;
            if (conteo == 3) {
                System.out.println("EQUIPO BLOQUEADO");
                break;
            }
        }
    } while (!exito);

    if (exito) {
        System.out.println("CONTRASEÑA CORRECTA");
    }

}
    
answered by 15.12.2018 в 15:57