Start learning free

LESSON 194 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V3 in Java: Four Character-Class Flags

Practice Read the full explanation

Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?

You start with:

String password = "Secret!99";

Build:

Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Start four flags (hasUpper, hasLower, hasDigit, hasSymbol) at false. Loop over every character and set the matching flag to true.
  • A symbol is any character that is not a letter and not a digit.
  • Print one line per flag, in this order: "Upper: ", "Lower: ", "Digit: ", "Symbol: ", each followed by the flag. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If the length is at least 8 and all four flags are true (stricter than the last lesson), print Strong; otherwise print Weak.

Try other passwords: with secret99 the output is

Length: 8
Upper: False
Lower: True
Digit: True
Symbol: False
Weak

Use Character.isUpperCase(c), isLowerCase, isDigit, isLetterOrDigit in password.toCharArray().

String password = "Secret!99";

int length = password.length();
System.out.println("Length: " + length);
boolean hasUpper = false;
boolean hasLower = false;
boolean hasDigit = false;
boolean hasSymbol = false;
for (char c : password.toCharArray()) {
    if (Character.isUpperCase(c)) {
        hasUpper = true;
    }
    if (Character.isLowerCase(c)) {
        hasLower = true;
    }
    if (Character.isDigit(c)) {
        hasDigit = true;
    }
    if (!Character.isLetterOrDigit(c)) {
        hasSymbol = true;
    }
}
System.out.println("Upper: " + (hasUpper ? "True" : "False"));
System.out.println("Lower: " + (hasLower ? "True" : "False"));
System.out.println("Digit: " + (hasDigit ? "True" : "False"));
System.out.println("Symbol: " + (hasSymbol ? "True" : "False"));
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
    System.out.println("Strong");
} else {
    System.out.println("Weak");
}

STEP 1 OF 13
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V3 in Java, explained

    Problem overview

    This version asks four questions about the password: does it contain an uppercase letter, a lowercase letter, a digit and a symbol? A symbol is any character that is neither a letter nor a digit, such as !, # or a space. The program prints the length, then one line per question, then a verdict. The verdict is Strong when the length is at least 8 and all four answers are yes, and Weak otherwise. The version before this one tracked a single flag, for digits.

    For Secret!99:

    Length: 9
    Upper: True
    Lower: True
    Digit: True
    Symbol: True
    Strong

    For secret99, which has no capital letter and no symbol:

    Length: 8
    Upper: False
    Lower: True
    Digit: True
    Symbol: False
    Weak

    Approach: four flags, one pass

    Each question gets its own boolean flag: hasUpper, hasLower, hasDigit and hasSymbol. All four start at false. One loop over the characters can update all of them, so the password is read once, not four times.

    Inside the loop, each class has its own if, with no else. A single character belongs to at most one of the first three classes, but every test still has to run, because the loop does not know in advance which class a character is in. Using else if would also work for most characters, but separate ifs are simpler to read and cannot skip a test by mistake.

    The symbol test is the odd one out: it is defined by exclusion. Character.isLetterOrDigit(c) is true for letters and digits, so !Character.isLetterOrDigit(c) is true for everything else.

    Step by step

    1. Store the length in length and print the Length: line.
    2. Create the first two flags, hasUpper and hasLower, as boolean variables set to false.
    3. Create the other two, hasDigit and hasSymbol, the same way.
    4. Start the loop over password.toCharArray(), with the char variable c.
    5. Uppercase test. Character.isUpperCase(c) sets hasUpper to true.
    6. Lowercase test. Character.isLowerCase(c) sets hasLower, in its own if after the first one.
    7. Digit test. Character.isDigit(c) sets hasDigit.
    8. Symbol test. !Character.isLetterOrDigit(c) sets hasSymbol.
    9. Print the first two answers after the loop, each as True or False, chosen with the conditional operator.
    10. Print the last two, keeping the order Upper, Lower, Digit, Symbol.
    11. Write the verdict. The condition has five parts joined with &&: the length and the four flags.
    12. Add the else, which prints Weak for every password that misses at least one condition.

    Reading the example

    In Secret!99 the characters are S, e, c, r, e, t, !, 9 and 9, nine in all. The S sets hasUpper, the next five letters set hasLower, the ! sets hasSymbol, and the first 9 sets hasDigit. No flag is ever set back to false, so the order of the characters does not matter: 9!tereS gives the same four answers. Only the final state of the flags is used.

    The solution in Java

    String password = "Secret!99";
    
    int length = password.length();
    System.out.println("Length: " + length);
    boolean hasUpper = false;
    boolean hasLower = false;
    boolean hasDigit = false;
    boolean hasSymbol = false;
    for (char c : password.toCharArray()) {
        if (Character.isUpperCase(c)) {
            hasUpper = true;
        }
        if (Character.isLowerCase(c)) {
            hasLower = true;
        }
        if (Character.isDigit(c)) {
            hasDigit = true;
        }
        if (!Character.isLetterOrDigit(c)) {
            hasSymbol = true;
        }
    }
    System.out.println("Upper: " + (hasUpper ? "True" : "False"));
    System.out.println("Lower: " + (hasLower ? "True" : "False"));
    System.out.println("Digit: " + (hasDigit ? "True" : "False"));
    System.out.println("Symbol: " + (hasSymbol ? "True" : "False"));
    if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
        System.out.println("Strong");
    } else {
        System.out.println("Weak");
    }
    

    The Java details worth a closer look:

    • The Character helpers. isUpperCase, isLowerCase, isDigit and isLetterOrDigit are static methods of the Character class. They take a char and return a boolean, so they can be used directly as if conditions.
    • The ! operator. It flips a boolean. Without it, the last if would set hasSymbol for letters and digits, the opposite of what is wanted.
    • Unicode. These methods understand letters beyond English. Character.isLetter('é') is true, so é counts as a letter here, not a symbol. The underscore is a symbol, because Character.isLetterOrDigit('_') is false.
    • Printing booleans. A boolean joined to a string appears as lowercase true, so each line picks its text with ? :. The parentheses around the conditional are required.
    • Long conditions. A chain of && is true only when every part is true, and it stops at the first false part.

    Complexity

    Time: O(n) for n characters: the loop makes four constant-time checks per character. Space: O(n) for the array from toCharArray(), plus a handful of variables.

    Edge cases and common mistakes

    • A space is a symbol. Secret 99 has length 9 and all four flags are True, so it is Strong. The space is the symbol.
    • Missing one class. Secret99 has length 8 and uppercase, lowercase and digits, but no symbol. It prints Symbol: False and Weak.
    • Too short, but all four classes. A password such as Ab1! has every class but only four characters, so it prints four True lines after Length: 4 and is still Weak.
    • Forgetting the ! on the symbol test. The flag would then say whether the password has a letter or digit, not a symbol.
    • Treating the rules as one condition. The verdict needs all five conditions, so a password that fails any single one, such as a missing symbol, is Weak however strong it looks otherwise. Testing the flags one at a time in separate if statements would print several verdicts instead of one.
    • Printing the flags inside the loop. The lines would repeat once per character. They belong after the closing brace.

    Testing

    • Secret!99 gives length 9, four True lines and Strong.
    • secret99 gives length 8, then Upper: False, Lower: True, Digit: True, Symbol: False and Weak.
    • Secret99 gives length 8, then True, True, True, False and Weak.
    • Secret 99 gives length 9, four True lines and Strong.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free