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
- Store the length. Create
val lengthfrompassword.length. It is aval, because the length of this password never changes. - Print the first line with
println: the textPassword length:followed bylength. - Write the
iffor the case wherelengthis at least 8, and printOKinside it. - Add the
elsebranch, which printsToo short. It runs for every length theifdid 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.lengthhas no parentheses. On aString,lengthis written without them, and the lesson uses it that way. The result is anInt.- String plus
Int."Password length: " + lengthconverts the number to text when it is added to a string, so no explicit conversion is needed. elseon 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.
lengthcounts 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.
abcdefghprintsPassword length: 8andOK. With>instead of>=it would be rejected. - One short of the limit.
abcdefghas 7 characters and printsToo short. - An empty password. The length is 0, the first line is
Password length: 0, and the verdict isToo short. - Printing the verdict outside the branches. If
println("OK")sits after theif/else, it runs every time, so a short password would print bothToo shortandOK. - Counting spaces. A space is a character like any other, so
a bhas a length of 3.
Testing
Change the password in the editor above and compare with this table:
| Password | Length line | Verdict |
|---|---|---|
Secret99 | Password length: 8 | OK |
abc | Password length: 3 | Too short |
abcdefg | Password length: 7 | Too short |
| (empty) | Password length: 0 | Too short |
The next version adds a second rule to this check.
