Start learning free

LESSON 195 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in Kotlin: 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 a property (no 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

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in Kotlin, explained

    Problem overview

    The first version of the password strength checker looks at one thing only: how long the password is. The program starts with val password = "Secret99". It prints the number of characters, then a verdict: OK if the password has at least 8 characters, Too short otherwise.

    For Secret99 the output is:

    Password length: 8
    OK

    For abc it is:

    Password length: 3
    Too short

    Length is a weak measure of strength on its own, but it is a useful first rule, and it introduces the pieces the later versions build on: reading a property of a string, printing a labelled value, and choosing between two outcomes.

    Approach: measure once, use twice

    The length is needed in two places: it appears in the first line of the output, and it decides the verdict. Computing it once and storing it in a val keeps the code short and means both uses are guaranteed to agree.

    The verdict is a two-way choice, so an if/else fits. The question to settle is the boundary. "At least 8" includes 8, so the condition is length >= 8. A password of exactly 8 characters passes, and one of 7 fails.

    Step by step

    1. Store the length. Create a val named length from password.length. In Kotlin, length is a property of String, so it is written without parentheses.
    2. Print the first line. Join the label "Password length: " and the number with +, and pass the result to println. Kotlin turns the Int into text when the left side of + is a string.
    3. Write the passing case. An if with the condition length >= 8 prints OK.
    4. Write the failing case. An else prints Too short. It covers every length the if rejected, which is 7 or fewer.

    The solution in Kotlin

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

    Kotlin details worth a closer look:

    • length is a property, not a method. password.length is correct. Writing password.length() is a compile error, because an Int can't be called like a function.
    • Counting characters. length counts UTF-16 code units, which for ordinary letters, digits and common symbols is the same as the number of characters. A few rare characters, such as many emoji, take two units and count as 2.
    • if and else are written with braces here. The else sits on the same line as the closing brace of the if. This is a style, not a rule of the language, but it is the common Kotlin layout.
    • val versus var. length never changes after it is set, so it is a val.
    • Mixing text and numbers. "Password length: " + length works because the string comes first. The reverse order, length + "...", doesn't compile when length is an Int.

    Edge cases and common mistakes

    • The boundary at 8. Writing length > 8 would reject an 8-character password, which the rules accept. A 7-character password such as abcdefg must print Too short.
    • Counting the label. The number printed is the length of the password only, not of the whole output line.
    • Printing both verdicts. Without the else, adding a second plain if for the short case works, but the two conditions then have to be kept in sync by hand. The else makes the second branch automatic.
    • The empty password. An empty string has length 0, so the program prints a length of 0 and Too short.

    Testing

    Change the starting password and check the results. The 7-character abcdefg, one below the limit, gives:

    Password length: 7
    Too short

    An empty password, "", gives:

    Password length: 0
    Too short

    And the 8-character Secret99 gives OK, as shown at the top. These three cases cover both sides of the boundary and the extreme at zero. The next version builds on this one by adding a second rule.

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