I'm trying to create a password verification routine, my problem is when a input is entered it will go through each if statement individually. I'm trying to restart validation when a condition is not met. At the moment it only jumps to the next if statement when the previous condition is met, even though its a new input. Thanks in advance.
Well done finding the code button, but you need to indent your code to make it legible. Remember that you can confuse yourself with unindented code.
Why are you caling nextLine() so often (lines 16, 23, 28, 33, 38, and 43)? What will happen if one of the tests fails? I think you will get a new password and then only do part of the validation on it. Or, if you don't enter something, your program will appear to hang. I think those multiple ifs would be better as else‑ifs.
What does line 46 mean? It looks like the end of a do loop, but it means you will never reenter the loop, so your loop will always run once. In which case, what is the point of having a loop in the first place?
Don't write if (something) return true; else return false; See the old Sun Style Guide (§10.5.2).Give that method private access. Its name doesn't tell us what it does.
posted 4 weeks ago
Thanks for the heads up. I'm basically trying to get the loop to re run when 1 of the conditions is not met. At the moment it loops through individually. I tried the multiple next.() to try get back to the start of the verification. Line 46 is also an attempt to re run the if statements when one if the conditions are false.
Think about simplifying things a bit. What do you really want to do? You want to keep looping until some condition is false. so, write that:
now, in my mind, "validate input" is a bit broad to be a line of code...so let's make it a method. and there is no reason to call the method, save the validity, and then use that in the while statement, so we can simplify the above. i'm going to call a boolean method in the while :
so now i need to write an isValid() method that returns a boolean. It needs the string to test. Note - now i need to update the line where I call this, too...but that's how it goes. You keep adding one piece at a time.
so now I need to fill in that body. My preference would be to write a single method to test each requirement, then call them one by one. I would STRONGLY advice writing one at a time, test it, get it working, etc. before you start adding the second. Repeat with testing it, etc. before starting the third.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors