Question: It seems my code won't work while looping. It's a simple password verifier, however entering subsequent passwords will return the same invalid as the previous password. The simple answer might be to set boolean values under the do so they reset everytime the loop runs. But if I do that then the while doesn't recognize the values. I've also tried just setting the while to false but that causes the loops output to be all screwy. Any ideas here?

import java.util.Scanner;

public class Pass
{
        public static void main(String[] args)
        {

                boolean invalidLength = false;                            // Boolean for checking length
                boolean containsRestrictedWord = false;                   // Boolean for checking for restrictd words
                boolean containsLowerCaseLetter = false;                  // Boolean for checking for a lower case letter
                boolean containsUpperCaseLetter = false;                  // Boolean for checking for a upper case letter
                boolean containsDigit = false;                            // Boolean for checking for a numberical digit
                boolean containsSpecialChar = false;                      // Boolean for checking for special characters

                Scanner stdIn = new Scanner(System.in);

            do                                                            // Loop until user enters a valid password
            {

                System.out.println("Password Verifier");
                System.out.println("\nEnter a password that meets the following rules:\n" +
                                "\tIs atleast 8 characters long\n" +
                                "\tContains atleast 1 lower letter character\n" +
                                "\tContains atleast 1 upper letter character\n" +
                                "\tContains atleast 1 numberic digit\n" +
                                "\tContains atleast 1 special character from the set: !@#$%^&*\n" +
                                "\tDoes not contain the word \"and\" or the word \"end\"\n");
                System.out.print("\nEnter your password: ");

                String password = stdIn.nextLine();

                for(int i = 0; i < password.length(); i++)
                {
                        char ch = password.charAt(i);

                                                                          // If the character is upper case
                        if(Character.isUpperCase(ch) && !containsUpperCaseLetter)
                        {
                                containsUpperCaseLetter = true;
                        }

                                                                          // If the character is lower case
                        if(Character.isLowerCase(ch) && !containsLowerCaseLetter)
                        {
                                containsLowerCaseLetter = true;
                        }

                                                                          // If the character is a digit
                        if(Character.isDigit(ch) && !containsDigit)
                        {
                                containsDigit = true;
                        }

                                                                          // If the character is a special character
                        if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&' || ch == '*') && !containsSpecialChar)
                        {
                                containsSpecialChar = true;
                        }
                }

                                                                          // Check to make sure the user input a password of at least 8 characters
                if(password.length() < 8 )
                {
                        invalidLength = true;
                }

                                                                          // Check to make sure the user input does not contain the restricted words
                if(password.contains("and") || password.contains("end"))
                {
                        containsRestrictedWord = true;
                }

                                                                          // Print out any possible rules the user might have broken
                if(invalidLength || containsRestrictedWord || !containsDigit || !containsLowerCaseLetter || !containsUpperCaseLetter || !containsSpecialChar)
                {
                    System.out.println("\nInvalid");
                }
                if(invalidLength) { System.out.println("Must be at least 8 characters long"); }
                if(containsRestrictedWord) { System.out.println("Contains the string \"and\" or \"end\""); }
                if(!containsDigit) { System.out.println("Must contain a numeric digit"); }
                if(!containsLowerCaseLetter) { System.out.println("Does not contain at least one lowercase letter"); }
                if(!containsUpperCaseLetter) { System.out.println("Does not contain at least one uppercase letter"); }
                if(!containsSpecialChar) { System.out.println("Must contain a special character"); }
                System.out.println("\n-------------------------------------------------------\n");
            } while (invalidLength || containsRestrictedWord || !containsDigit || !containsLowerCaseLetter || !containsUpperCaseLetter || !containsSpecialChar);

                System.out.println("Valid");
        }                                                                 // End Main
}                                                                         // End public class Pass

also a thought:

if(Character.isUpperCase(ch) && !containsUpperCaseLetter)
                        {
                                containsUpperCaseLetter = true;
                        }

why this second condition ? are you afraid you'll overwrite true with true ?
the above can be made shorter by just putting:

if(Character.isUpperCase(ch) )
                        {
                                containsUpperCaseLetter = true;
                        }

one time, this isn't that much, but you are forcing your code to run checks you don't need practically each time you check a value.

It might also be easier to create a simple regex to check for the validation.

I appreciate that. I'm new with Java trying to learn though. How do I re-initialize the booleans? That was sort of my initial question

boolean isAnExample;
...
do {  // Loop until user enters a valid password
   isAnExample = true; // re-sets boolean at start of each pass of the loop
   ...

Edited 1 Year Ago by JamesCherrill

James,

Such an easy concept yet I never thought about that. Thanks a lot everyone who posted. You all are life savers!

This question has already been answered. Start a new discussion instead.