I am attempting to make it so this program runs again if the user enters 'y'. What happens when the user does this is the program runs again, but not correctly. This is what I have so far. I know a do while loop will be best for this. Is it an error in how the variables are initialized?
String password;
boolean hasLength;
boolean hasUppercase;
boolean hasLowercase;
boolean hasDigit;
boolean hasSpecial;
String runAgain = "";
Scanner scan = new Scanner(System.in);
/******************************************************************************
* Inputs Section *
******************************************************************************/
do {
System.out.println("A password must be at least 8 character long");
System.out.println("And must contain:");
System.out.println("-At least 1 number");
System.out.println("-At least 1 uppercase letter");
System.out.println("-At least 1 special character (!@#$%^&*()_+)\n");
System.out.print("Please enter your new password: ");
password = scan.nextLine();
/******************************************************************************
* Processing Section *
******************************************************************************/
System.out.print("\n");
System.out.println("Entered Password:\t" + password);
hasLength = password.length() < 8; // parameters for length
// for lower and uppercase characters
hasUppercase = !password.equals(password.toLowerCase()); //lowercase version of the password equals the password,
hasLowercase = !password.equals(password.toUpperCase()); //only if it does not contain uppercase letters.
hasDigit = password.matches(".*[0-9].*");//checks for digits
hasSpecial = !password.matches("[A-Za-z0-9]*"); //for anything not a letter in the ABC's
//prints the verdict
System.out.print("Verdict: ");
if(hasLength)
{
System.out.println("\t\tInvalid, Must have at least 8 characters");
}
if(!hasUppercase)
{
System.out.println("\t\t\tInvalid, Must have an uppercase character");
}
if(!hasLowercase)
{
System.out.println("\t\t\tInvalid, Must have a lowercase character");
}
if(!hasDigit)
{
System.out.println("\t\t\tInvalid, Must have a number");
}
if(!hasSpecial)
{
System.out.println("\t\t\tInvalid, Must have a special character");
}
System.out.print("Would you like to make another password? (Y/N) ");
runAgain = scan.next();
System.out.println("\n");
} while (runAgain.equalsIgnoreCase("Y"));
which gives this output when yes is entered to run again. It skips the prompt entirely.
Would you like to make another password? (Y/N) y
A password must be at least 8 character long
And must contain:
-At least 1 number
-At least 1 uppercase letter
-At least 1 special character (!@#$%^&*()_+)
Please enter your new password:
Entered Password:
Verdict: Invalid, Must have at least 8 characters
Invalid, Must have an uppercase Character
Invalid, Must have a lowercase character
Invalid, Must have a number
Invalid, Must have a special character
Would you like to make another password? (Y/N)