Start learning free

LESSON 192 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in Java: A Length Check

Practice Read the full explanation

A tiny password strength checker that looks only at the length of the password.

You start with:

String password = "Secret99";

Build:

Password length: 8
OK

Rules:

  • Print "Password length: " followed by the number of characters.
  • If the length is at least 8, print OK; otherwise print Too short.

Try other passwords: with abc the output is

Password length: 3
Too short

Java's String.length() is a method (with parentheses).

String password = "Secret99";

int length = password.length();
System.out.println("Password length: " + length);
if (length >= 8) {
    System.out.println("OK");
} else {
    System.out.println("Too short");
}

STEP 1 OF 4
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in Java, explained

    Problem overview

    This is the first version of a small password strength checker. It looks at one thing only: how many characters the password has. The program starts with String password = "Secret99";, prints the length on one line, and then prints a verdict on a second line: OK when the length is at least 8, otherwise Too short.

    For Secret99 the output is:

    Password length: 8
    OK

    For abc it is:

    Password length: 3
    Too short

    The next version builds on this one.

    Approach

    The length is used twice: it is printed, and it is compared with 8. So the program stores it in a variable once and uses the variable both times. Then a single if/else chooses between the two verdicts.

    The boundary is the part to get right. "At least 8" includes 8, so the test is length >= 8. A password of exactly 8 characters is accepted, and one of 7 is not.

    Step by step

    1. Store the length. Create an int named length and assign it password.length().
    2. Print the first line. Join the text Password length: and length with +, and pass the result to System.out.println.
    3. Write the if. When length >= 8, print OK.
    4. Add the else. It runs for every other length, that is 7 or fewer, and prints Too short.

    The solution in Java

    String password = "Secret99";
    
    int length = password.length();
    System.out.println("Password length: " + length);
    if (length >= 8) {
        System.out.println("OK");
    } else {
        System.out.println("Too short");
    }
    

    The Java details worth a closer look:

    • length() has parentheses. On a String, length is a method, so you write password.length(). On an array it is a field without parentheses, array.length. Mixing them up is a compile error.
    • The type of length is int. length() returns an int, so the variable has the same type.
    • Joining text and a number. When one side of + is a String, Java converts the other side to text. That turns "Password length: " + length into Password length: 8.
    • Braces and else. The lesson writes } else { on one line, which is the common Java style.
    • What the length counts. length() counts UTF-16 code units. For ordinary letters, digits and punctuation that equals the number of characters. A character outside the basic range, such as an emoji, takes two units: "a😀".length() is 3.

    Edge cases and common mistakes

    • Exactly 8 characters. abcdefgh prints Password length: 8 and OK. Writing length > 8 would reject it.
    • 7 characters. abcdefg prints Password length: 7 and Too short.
    • Empty password. "" has length 0, so the output is Password length: 0 and Too short. A null password is different: calling length() on it throws a NullPointerException.
    • Spaces count. A space is a character like any other, so it is included in the length.
    • Printing the verdict on the same line. The lesson wants two separate lines, so there are two println calls, not one that joins everything.
    • Repeating the condition by hand. A second if (length < 8) gives the same output here, but else guarantees that exactly one of the two branches runs, with no gap or overlap between them.

    Testing

    • Secret99 gives Password length: 8 and OK.
    • abc gives Password length: 3 and Too short.
    • abcdefgh gives Password length: 8 and OK.
    • abcdefg gives Password length: 7 and Too short.
    • The empty string gives Password length: 0 and Too short.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free