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
- Store the length. Create a
valnamedlengthfrompassword.length. In Kotlin,lengthis a property ofString, so it is written without parentheses. - Print the first line. Join the label
"Password length: "and the number with+, and pass the result toprintln. Kotlin turns theIntinto text when the left side of+is a string. - Write the passing case. An
ifwith the conditionlength >= 8printsOK. - Write the failing case. An
elseprintsToo short. It covers every length theifrejected, 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:
lengthis a property, not a method.password.lengthis correct. Writingpassword.length()is a compile error, because anIntcan't be called like a function.- Counting characters.
lengthcounts 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. ifandelseare written with braces here. Theelsesits on the same line as the closing brace of theif. This is a style, not a rule of the language, but it is the common Kotlin layout.valversusvar.lengthnever changes after it is set, so it is aval.- Mixing text and numbers.
"Password length: " + lengthworks because the string comes first. The reverse order,length + "...", doesn't compile whenlengthis anInt.
Edge cases and common mistakes
- The boundary at 8. Writing
length > 8would reject an 8-character password, which the rules accept. A 7-character password such asabcdefgmust printToo 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 plainiffor the short case works, but the two conditions then have to be kept in sync by hand. Theelsemakes 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.
