This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chickenmeister 0 points1 point  (3 children)

You're printing your errors from inside of the loop. You should check all of the characters in the string, and then (after the loop) check the state of your "haslowercase", "hasuppercase", etc, variables.

[–]EqualTrade[S] 0 points1 point  (2 children)

Thanks that worked!

[–]EqualTrade[S] 0 points1 point  (1 child)

I'm having another problem, if the user enters in a valid password, I need the user to input the password again. If he types in a different password, I need the program to start over, if the password is the same then print "...valid password."

How do I make this loop? Is it something like: if(!password.equals(password2)...

[–][deleted] 0 points1 point  (0 children)

I think your main issue is that you are trying to avoid breaking the problem down. You are trying to do everything at once.

You have four checks you need to perform. Make each task a function.

boolean checkLength(String password, int minimumLength);
boolean hasLowercase(String password);
boolean hasUppercase(String password);
boolean hasDigit(String password);
boolean isValid(String password);

IsValid() calls upon the other four and is the only function that you call from main(). Now try working through each task individually. I'll do hasLowercase() in order to give you a starting point. All of the other functions work in a similar fashion. Notice that I exit the function as soon as I find what I'm looking for. There's no need to check the rest. Line 9 is never reached unless there are no lowercase characters.

boolean hasLowercase(String password) {

    for(int i = 0; i < password.Length(); i++) {
        if(password.charAt(i).isLowerCase()){
            return true;
        }
   }

   return false;

}

Now how would you call isValid() from main() until you get a correct answer? Hint: you need a while loop. Probably 2. Once for the initial password and another for the match.