Problem overview
The password checker now has a second rule. A password is Strong only if it has at least 8 characters and contains at least one digit. Otherwise it is Weak. The version before this one only checked the length.
The program prints three lines: the length, whether a digit was found, and the verdict. For Secret99:
Length: 8
Has digit: True
Strong
For abcdefgh, which is long enough but has no digit:
Length: 8
Has digit: False
Weak
Note the capital letters in True and False: the output must show them that way.
Approach: a flag set during one pass
To find out whether a password has a digit, look at each character. A flag is a boolean variable that starts at false, meaning "no digit seen yet". The loop visits every character, and whenever it meets a digit it sets the flag to true. After the loop the flag answers the question for the whole password.
The flag only ever changes in one direction. Nothing in the loop sets it back to false, so one digit anywhere is enough, and later non-digit characters cannot undo it.
The verdict then combines two facts with &&: length >= 8 and hasDigit. Both must hold for Strong.
Step by step
- Store the length in an
intnamedlength, used for the printed line and for the 8-character rule. - Print
Length:followed by the number. - Create the flag.
boolean hasDigit = false;assumes there is no digit until one is seen. - Loop over the characters.
password.toCharArray()turns the string into achar[], and the enhancedforgives each character tocin turn. - Test each character. Inside the loop,
Character.isDigit(c)istruefor a digit, and then the flag is set totrue. - Print the flag after the loop. A
booleanprints as lowercasetrue, so the conditional operatorhasDigit ? "True" : "False"picks the text with the capital letter. - Write the verdict. When
length >= 8 && hasDigit, printStrong. - Add the
else. It catches everything else: too short, no digit, or both. It printsWeak.
How the flag changes
For Secret99 the loop visits S, e, c, r, e and t, and hasDigit stays false for all six. At the first 9 the if fires and the flag becomes true. The second 9 sets it to true again, which changes nothing. When the loop ends, the flag is true, and the line Has digit: True is printed once, outside the loop.
The loop keeps reading characters after it has found a digit. You could stop early with break, but passwords are short and the lesson keeps the loop as simple as possible. The same pattern, a flag that starts as false and is set inside a loop, is the standard way to ask "does anything in this sequence match?".
The solution in Java
String password = "Secret99";
int length = password.length();
System.out.println("Length: " + length);
boolean hasDigit = false;
for (char c : password.toCharArray()) {
if (Character.isDigit(c)) {
hasDigit = true;
}
}
System.out.println("Has digit: " + (hasDigit ? "True" : "False"));
if (length >= 8 && hasDigit) {
System.out.println("Strong");
} else {
System.out.println("Weak");
}
The Java details worth a closer look:
toCharArray()andchar. AStringcannot be used directly in an enhancedfor. Converting it to achar[]gives something the loop can walk through. Acharis a single UTF-16 unit, written in single quotes in source code, as in'a'.- The parentheses around the conditional. In
"Has digit: " + (hasDigit ? "True" : "False")they matter. Without them,+binds tighter than?:, so Java would try to use the whole string"Has digit: " + hasDigitas the condition, which does not compile because it is not aboolean. - Printing a boolean.
"Has digit: " + hasDigitgivesHas digit: truein lowercase, which does not match the required output. Character.isDigit. It also accepts digits from other scripts, for example the Arabic-Indic digit three, soCharacter.isDigit('٣')istrue. For ordinary passwords it behaves as "0 to 9".&&short-circuits. Iflength >= 8is false, Java does not look athasDigit. Here both operands are plain variables, so the order does not change the result.
Complexity
Time: O(n) for a password of n characters, because the loop looks at each character once. Space: O(n), since toCharArray() builds a new array holding a copy of the characters. Calling password.charAt(i) in an index loop would avoid that copy, but the lesson's version is clearer to read.
Edge cases and common mistakes
- Long enough, no digit.
abcdefghisWeak, as shown above. - A digit, but short.
Short1printsLength: 6,Has digit: TrueandWeak. - Digits only.
12345678printsLength: 8,Has digit: TrueandStrong. This version checks only a length and a digit, so letters are not required. - Setting the flag back to
falsein the loop. Anelse { hasDigit = false; }would make the flag reflect only the last character. - Declaring the flag inside the loop. It would be reset on every pass and invisible after the loop. Declare it before the loop.
- Using
||instead of&&. The password would beStrongwith either rule satisfied, so a long password without a digit would pass.
Testing
Secret99givesLength: 8,Has digit: True,Strong.abcdefghgivesLength: 8,Has digit: False,Weak.Short1givesLength: 6,Has digit: True,Weak.12345678givesLength: 8,Has digit: True,Strong.
