Start learning free

LESSON 195 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in Scala: Length Check

Practice Read the full explanation

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

You start with:

val 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

password.length is written without parentheses.

val password = "Secret99"

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

STEP 1 OF 4
    Ctrl + Enter to check

    SCALA · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in Scala, explained

    Problem overview

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

    For Secret99 the output is:

    Password length: 8
    OK

    For abc it is Password length: 3 followed by Too short.

    Approach

    The program needs the length twice: once to print it and once to compare it with 8. Storing it in a named value avoids asking the string for its length twice and gives the number a readable name. The rest is one decision with two outcomes, which is what if/else is for.

    The rule says "at least 8", and that includes 8. A password of exactly 8 characters is acceptable, so the comparison is >=.

    Step by step

    1. Store the length. Create val length from password.length. It is a val, because the length of this password never changes.
    2. Print the first line with println: the text Password length: followed by length.
    3. Write the if for the case where length is at least 8, and print OK inside it.
    4. Add the else branch, which prints Too short. It runs for every length the if did not take, which is 7 or fewer.

    The solution in Scala

            val password = "Secret99"
    
            val length = password.length
            println("Password length: " + length)
            if (length >= 8) {
                println("OK")
            } else {
                println("Too short")
            }

    A few Scala details:

    • password.length has no parentheses. On a String, length is written without them, and the lesson uses it that way. The result is an Int.
    • String plus Int. "Password length: " + length converts the number to text when it is added to a string, so no explicit conversion is needed.
    • else on the closing brace's line. Writing } else { keeps the two blocks visibly tied together. It is the style used throughout the lessons.
    • Characters, not bytes. length counts the 16-bit characters of the string. For ordinary letters, digits and symbols this is the number of characters you see, which is all this lesson needs.

    Comparisons in Scala

    The condition inside if (...) must be a Boolean. Scala's comparison operators are >, >=, <, <=, == and !=, and they work the same way on numbers as in most languages. This check could just as well be written the other way round, testing length < 8 and printing Too short first. The two forms are equivalent, and the lesson keeps the success case first because that is how the rule reads: "at least 8 is OK".

    Only one of the two branches runs. When the condition is true, the else block is skipped, and when it is false, the first block is skipped. That is why the verdict is always exactly one line.

    Edge cases and common mistakes

    • Exactly 8 characters. abcdefgh prints Password length: 8 and OK. With > instead of >= it would be rejected.
    • One short of the limit. abcdefg has 7 characters and prints Too short.
    • An empty password. The length is 0, the first line is Password length: 0, and the verdict is Too short.
    • Printing the verdict outside the branches. If println("OK") sits after the if/else, it runs every time, so a short password would print both Too short and OK.
    • Counting spaces. A space is a character like any other, so a b has a length of 3.

    Testing

    Change the password in the editor above and compare with this table:

    PasswordLength lineVerdict
    Secret99Password length: 8OK
    abcPassword length: 3Too short
    abcdefgPassword length: 7Too short
    (empty)Password length: 0Too short

    The next version adds a second rule to this check.

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