Start learning free

LESSON 169 OF 410 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 - Scored 0-100 in Java

Practice

Combine the earlier checks into a single numeric score between 0 and 100.

You start with:

String password = "Secret!99";

Build:

Length: 9
Score: 100

Rules:

  • Start with score = 0.
  • If the length is at least 8, add 25.
  • If the password contains an uppercase letter, add 20.
  • If it contains a lowercase letter, add 20.
  • If it contains a digit, add 20.
  • If it contains a symbol (not a letter and not a digit), add 15.
  • Print "Length: " followed by the length, then print "Score: " followed by the score.

Try other passwords: with abc123 the output is

Length: 6
Score: 40

Use the same Character checks, then accumulate into int score.

String password = "Secret!99";

int length = password.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;
    }
}
int score = 0;
if (length >= 8) {
    score += 25;
}
if (hasUpper) {
    score += 20;
}
if (hasLower) {
    score += 20;
}
if (hasDigit) {
    score += 20;
}
if (hasSymbol) {
    score += 15;
}
System.out.println("Length: " + length);
System.out.println("Score: " + score);

STEP 1 OF 18
    Ctrl + Enter to check

    Password Strength V6 in Java

    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free