Start learning free

LESSON 192 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V2 in Java: Checking for a Digit

Practice Read the full explanation

Extend the length check with a second rule: the password must also contain at least one digit.

You start with:

String password = "Secret99";

Build:

Length: 8
Has digit: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Loop through each character to set a boolean flag hasDigit, which starts as false and becomes true when a digit is seen.
  • Print "Has digit: " 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 hasDigit is true, print Strong; otherwise print Weak.

Try other passwords: with abcdefgh the output is

Length: 8
Has digit: False
Weak

Character.isDigit(c) tests a single char. Use (hasDigit ? "True" : "False") to print a capitalised True/False.

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");
}

STEP 1 OF 8
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V2 in Java, explained

    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

    1. Store the length in an int named length, used for the printed line and for the 8-character rule.
    2. Print Length: followed by the number.
    3. Create the flag. boolean hasDigit = false; assumes there is no digit until one is seen.
    4. Loop over the characters. password.toCharArray() turns the string into a char[], and the enhanced for gives each character to c in turn.
    5. Test each character. Inside the loop, Character.isDigit(c) is true for a digit, and then the flag is set to true.
    6. Print the flag after the loop. A boolean prints as lowercase true, so the conditional operator hasDigit ? "True" : "False" picks the text with the capital letter.
    7. Write the verdict. When length >= 8 && hasDigit, print Strong.
    8. Add the else. It catches everything else: too short, no digit, or both. It prints Weak.

    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() and char. A String cannot be used directly in an enhanced for. Converting it to a char[] gives something the loop can walk through. A char is 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: " + hasDigit as the condition, which does not compile because it is not a boolean.
    • Printing a boolean. "Has digit: " + hasDigit gives Has digit: true in lowercase, which does not match the required output.
    • Character.isDigit. It also accepts digits from other scripts, for example the Arabic-Indic digit three, so Character.isDigit('٣') is true. For ordinary passwords it behaves as "0 to 9".
    • && short-circuits. If length >= 8 is false, Java does not look at hasDigit. 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. abcdefgh is Weak, as shown above.
    • A digit, but short. Short1 prints Length: 6, Has digit: True and Weak.
    • Digits only. 12345678 prints Length: 8, Has digit: True and Strong. This version checks only a length and a digit, so letters are not required.
    • Setting the flag back to false in the loop. An else { 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 be Strong with either rule satisfied, so a long password without a digit would pass.

    Testing

    • Secret99 gives Length: 8, Has digit: True, Strong.
    • abcdefgh gives Length: 8, Has digit: False, Weak.
    • Short1 gives Length: 6, Has digit: True, Weak.
    • 12345678 gives Length: 8, Has digit: True, Strong.
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free