Start learning free

LESSON 194 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V4 in Java: Counting Character Classes

Practice Read the full explanation

Count how many characters of each class the password contains.

You start with:

String password = "Hello!World9";

Build:

Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1

Rules:

  • Use four integer counters, all starting at 0.
  • Loop through each character and add 1 to the matching counter.
  • Treat any character that is not a letter and not a digit as a symbol.
  • Print one line per counter, in this order: "Uppercase: ", "Lowercase: ", "Digits: ", "Symbols: ", each followed by the count.

Try other passwords: with Pass word!2 the output is

Uppercase: 1
Lowercase: 7
Digits: 1
Symbols: 2

Character.isUpperCase, isLowerCase, isDigit, isLetterOrDigit, with ++ counters.

String password = "Hello!World9";

int uppercase = 0;
int lowercase = 0;
int digits = 0;
int symbols = 0;
for (char c : password.toCharArray()) {
    if (Character.isUpperCase(c)) {
        uppercase++;
    }
    if (Character.isLowerCase(c)) {
        lowercase++;
    }
    if (Character.isDigit(c)) {
        digits++;
    }
    if (!Character.isLetterOrDigit(c)) {
        symbols++;
    }
}
System.out.println("Uppercase: " + uppercase);
System.out.println("Lowercase: " + lowercase);
System.out.println("Digits: " + digits);
System.out.println("Symbols: " + symbols);

STEP 1 OF 13
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V4 in Java, explained

    Problem overview

    The checker now counts instead of only detecting. For a password, the program reports how many uppercase letters, lowercase letters, digits and symbols it contains. A symbol is any character that is not a letter and not a digit, and that includes a space. The earlier version used four boolean flags that said whether each class was present; this one uses four int counters that say how many.

    For Hello!World9 the output is:

    Uppercase: 2
    Lowercase: 8
    Digits: 1
    Symbols: 1

    For Pass word!2, which contains a space and an exclamation mark:

    Uppercase: 1
    Lowercase: 7
    Digits: 1
    Symbols: 2

    Approach: four counters, one pass

    A counter is an int that starts at 0 and goes up by one each time a matching character is found. The structure is the same as with flags: one loop over the characters, and one if per class inside it. The only change is the action. Instead of hasUpper = true the code runs uppercase++.

    That small change loses nothing: a flag can be derived from a counter, because a class is present exactly when its counter is above 0. A counter keeps more information than a flag.

    The four tests are independent if statements. A character that is an uppercase letter satisfies only the first, a digit only the third, and a space only the last, so every character adds to exactly one counter. That gives a useful check: the four counters always add up to the length of the password.

    Step by step

    1. Create the four counters one by one: uppercase, lowercase, digits and symbols, all int variables set to 0. Each class has its own counter.
    2. Start the loop. The char variable c takes each character of password.toCharArray(), once per character, 12 times for Hello!World9.
    3. Uppercase. When Character.isUpperCase(c) is true, run uppercase++.
    4. Lowercase. When Character.isLowerCase(c) is true, run lowercase++.
    5. Digits. When Character.isDigit(c) is true, run digits++.
    6. Symbols. When !Character.isLetterOrDigit(c) is true, run symbols++. Letters and digits make this false; everything else, a space included, counts.
    7. Print the four lines after the loop, in the order Uppercase, Lowercase, Digits, Symbols. Each is a label joined to its counter with +.

    The solution in Java

    String password = "Hello!World9";
    
    int uppercase = 0;
    int lowercase = 0;
    int digits = 0;
    int symbols = 0;
    for (char c : password.toCharArray()) {
        if (Character.isUpperCase(c)) {
            uppercase++;
        }
        if (Character.isLowerCase(c)) {
            lowercase++;
        }
        if (Character.isDigit(c)) {
            digits++;
        }
        if (!Character.isLetterOrDigit(c)) {
            symbols++;
        }
    }
    System.out.println("Uppercase: " + uppercase);
    System.out.println("Lowercase: " + lowercase);
    System.out.println("Digits: " + digits);
    System.out.println("Symbols: " + symbols);
    

    The Java details worth a closer look:

    • ++. uppercase++ adds 1 to the variable. It is shorthand for uppercase = uppercase + 1. As a statement on its own, ++uppercase does the same thing.
    • Counters must start at a value. A local variable that is declared but not assigned cannot be read. int uppercase; followed by uppercase++ does not compile, because the compiler says the variable might not have been initialized. That is why every counter is set to 0 up front.
    • Four ifs, not an else chain. The symbol test is a negative one, defined as "not a letter and not a digit". It does not need to know which class a letter belongs to, so the tests stay independent of each other.
    • Wide characters. The loop works on UTF-16 units, so a character outside the basic range, such as an emoji, is two units. Each unit is tested separately and neither is a letter or digit, so such a character adds 2 to symbols.
    • Naming. The counter digits and the method Character.isDigit are unrelated names. The variable is just an int.

    Complexity

    Time: O(n) for a password of n characters, with four constant-time tests per character. Space: O(n), because toCharArray() copies the characters into a new array; the counters themselves take constant space.

    Edge cases and common mistakes

    • Empty password. The loop never runs and all four counters print 0.
    • Spaces. A space is not a letter or a digit, so it counts as a symbol. In Pass word!2 the space and the ! make Symbols: 2.
    • Counting the total. In Hello!World9, 2 + 8 + 1 + 1 equals 12, the length. If your four counters do not add up to the length, a character was counted twice or not at all.
    • Forgetting to start at 0. The compiler rejects an uninitialized counter, as described above.
    • Printing inside the loop. Each line would be printed once per character. The printing code belongs after the loop.
    • Wrong order of the output lines. The lines must appear as Uppercase, Lowercase, Digits, Symbols, with the exact labels, including the plural Digits and Symbols.
    • Using = instead of ++. uppercase = 1 would only remember that something was found, which is a flag again, not a count.

    Testing

    • Hello!World9 gives 2, 8, 1 and 1.
    • Pass word!2 gives 1, 7, 1 and 2.
    • The empty string gives 0, 0, 0 and 0.
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free