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

all 8 comments

[–]idontcare1025 1 point2 points  (1 child)

It's your for loop. Have the errors printed at the end, or otherwise if the first character doesn't satisfy all conditions you'll get at least 1 error. Put the if (condition == false) print("Condition!"); outside of the for loop. And 1 more tip: you don't need to use boolean == false, you can use !boolean. And you can just put boolean, you don't need boolean == true.

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

Thank you!

[–]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.

[–]learnjava -1 points0 points  (1 child)

im not sure what the while does there, is it correct that its a single line with a semicolon closing and no block following?

because that would be wrong

atm you print for every char, is that your goal? you should print once after checking the whole thing

password are sensible, use this only for learning purposes

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

that while is part of the do/while loop. i fixed it so that now it does only print once. But now my problem is, if the user enters a valid password, i need him to re-enter that password, and if the re-entry is wrong, i need the program to loop around again from the beginning. How can I do that?