简体   繁体   中英

Java looping to an original statement

I am new to java and need some help with some syntax issues. Basically what I have is a prompt which asks users to enter a password. If they enter a password, then the string password entered is displayed and the password is saved. What I need help with is if the user does not enter any string but just hits the enter button, I want the original prompt to appear. I want it to keep looping till a password is entered. Below is my original code, and what I tried with a for loop. I need some help. Thank you

    String prompt = "Enter password ";
    String pw = passwordProvider.getPassword(prompt);


     if (pw != null && pw.length() > 0)
     {
         passwordProvider.status("Password Updated");

       } else {

         System.out.println("here is where I want my original prompt to appear again");
     }

return DRPwCrypt.encryptAES(pw);

Code with for loop

     String prompt = "Enter password ";
     String pw = passwordProvider.getPassword(prompt);

     if (pw != null && pw.length() > 0)
     {
         passwordProvider.status("Password Updated");
     } else {

         System.out.println("here is where I want my original prompt to appear again");
     }

CODE WITH FOR LOOP

 for (
    String prompt = "Entry " + propertyName + " found in config " + configName +
                    "\nEnter new password : ";) {
String pw = passwordProvider.getPassword(prompt);


if (pw != null && pw.length() > 0)
{
    passwordProvider.status("Updating password");
} else {
    System.out.println("etsting");
}

return DRPwCrypt.encryptAES(pw);

}

The for loop is usually used when you want to iterate a predefined number of times.

In your case, a while loop would be a better option:

boolean correctPassword = false;
while(!correctPassword) {

     String prompt = "Enter password ";
     String pw = passwordProvider.getPassword(prompt);

     if (pw != null && pw.length() > 0)
     {
         passwordProvider.status("Password Updated");
         correctPassword = true;
     } else {

         System.out.println("Invalid password, please try again");
     }
}

while loops are used when you want to keep on iterating until a given condition is true or false (as the case may be).

The code above could also be modified to use a for loop instead:

boolean correctPassword = false;
for(;!correctPassword;) {

     String prompt = "Enter password ";
     String pw = passwordProvider.getPassword(prompt);

     if (pw != null && pw.length() > 0)
     {
         passwordProvider.status("Password Updated");
         correctPassword = true;
     } else {

         System.out.println("Invalid password, please try again");
     }
}

The above should create an infinite for loop which will keep on iterating until the flag is true . As you can see, this might be less readable and/or more confusing than the while loop.

EDIT: This code works as epxected for me:

import java.util.*;

public class HelloWorld{

     public static void main(String []args){
        boolean correctPassword = false;
        while(!correctPassword) {
             Scanner input = new Scanner(System.in);
             String prompt = "Enter password ";
             String pw = input.nextLine();
             System.out.println(pw.length());

             if (pw != null && pw.length() > 0)
             {
                 System.out.println("Password Updated");
                 correctPassword = true;
             } else {

                 System.out.println("Invalid password, please try again");
             }
        }
     }
}

You can also try using Java's Console class

Try this:

public static void main(String[] args) {

    Console console = System.console();
    if(console == null){
        System.out.println("ERROR: Couldn't get Console");
        return;
    }
    boolean passwordUpdated = false;
    while (!passwordUpdated) {

        System.out.print("Enter password :");
        String password = console.readLine();
        // char[] password = console.readPassword() // can be used

        if (password != null && password.length() > 0) {
            System.out.println("Password Updated");
            passwordUpdated = true;
        } else {
            System.out.println("WARN: Invalid password, please try again");
        }
    }
}

Note: you have to use terminal/command-prompt to run the program, on eclipse System returns null console object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM