samedi 5 août 2017

java- How do I create a String check with a delay of a certain amount of time?

I am learning java and so far I have created a password check using if statements. However I inserted my working String check into a while loop and added Thread.sleep(3000); for a 3 second delay, however once I completed that my GUI just keeps lagging and freezing on one page as if the button was pressed. Can somebody please show me how to make a working example of a code with a String check and after a certain amount of tries a delay to stop the user from trying again? (here is what I have:)

    //var declaration
    boolean match = false;
    String1 = "hi";
    String2 = (I know this is not code but just to omit some code:) userInput
    int time = 3000;
    int attempt = 0;
    //check
    while(!match && attempt < (maximumTries+1)){
        if(String1.equals(String2)){
            System.out.print("match");
        }
        else if(attempt < 11){
            attempt++;
            System.out.println("Failed:" + attempt);
        }
        else{
            attempt++;
            System.out.println("Please try again later you have:" + attempt + "failed attempts");
            try{
                Thread.sleep(time);
            }
            catch(InterruptedException ex) {
                Logger.getLogger(PasswordEntry.class.getName()).log(Level.SEVERE, null, ex);
            }
            time = time + 1000;//1 second more every time
        }
    }

Aucun commentaire:

Enregistrer un commentaire