user3313912
user3313912

Reputation: 17

Reset Boolean values?

I am trying to get my password verifier to loop until a correct response is give. It's looping right now, but its not resetting the boolean values after the first iteration, resulting in a valid response the second time no matter which password. How do I make the loop check for every value, or reset the table every time?

            boolean atLeast8 = false;                   
        boolean oneLower = false;                   
        boolean oneUpper = false;                   
        boolean oneNumber = false;                  
        boolean oneSpecial = false;                 
        boolean noAnd = false;                      
        boolean noEnd = false;  


            do
        {
            System.out.println("Enter your password. " + '\n');
            password = input.nextLine();
            System.out.println();




            for(int i=0; i<password.length(); i++)      //Checks for values througout the length of the string

            {
                char c = password.charAt(i);
                if(password.length() >= 8)
                atLeast8 = true;

                if(Character.isLowerCase(c))
                oneLower = true;

                if(Character.isUpperCase(c))
                oneUpper = true;

                if(Character.isDigit(c))
                oneNumber = true;

                if(c=='!' || c=='@' || c=='#' || c=='$' || c=='%' || c=='^' || c=='&' || c=='&' || c=='*')
                oneSpecial = true;

                if (password.indexOf("and") < 0)
                noAnd = true;

                if (password.indexOf("end") < 0)
                noEnd = true;

            }
            if(atLeast8 && oneLower && oneUpper && oneNumber && oneSpecial && noAnd && noEnd)   //Does the string contain any true values?
                {
                    System.out.println("Valid.");
                }
            else
                {
                    System.out.println("Invalid!");                                                 //Does the string contain any false values?
                }

                if(!atLeast8)
                {
                    System.out.println("Must be at least 8 characters long.");


                }
                if(!oneLower)
                {
                    System.out.println("Must contain one lower case letter.");

                }
                if(!oneUpper)
                {
                    System.out.println("Must contain one upper case letter.");

                }
                if(!oneNumber)
                {
                    System.out.println("Must contain one numeric digit.");

                }
                if(!oneSpecial)
                {
                    System.out.println("Must contain one special character.");

                }
                if(!noAnd)
                {
                    System.out.println("Must not contain the word 'and'.");

                }
                if(!noEnd)
                {
                    System.out.println("Must not contain the word 'end'.");

                }
        }while (atLeast8 == false || oneLower == false || oneUpper == false || oneNumber == false || oneSpecial == false || noAnd == false || noEnd == false);
    }

}

Upvotes: 1

Views: 4473

Answers (1)

pscs
pscs

Reputation: 642

Can't you just put

    atLeast8 = false;                   
    oneLower = false;                   
    oneUpper = false;                   
    oneNumber = false;                  
    oneSpecial = false;                 
    noAnd = false;                      
    noEnd = false;  

at the start of the loop (somewhere between the "do {" and the "for")

Or, is that being too simplistic?

Upvotes: 1

Related Questions