Problem overview
This is the first version of a small password strength checker. It looks at one thing only: how many characters the password has. The program starts with String password = "Secret99";, prints the length on one line, and then prints a verdict on a second line: OK when the length is at least 8, otherwise Too short.
For Secret99 the output is:
Password length: 8
OK
For abc it is:
Password length: 3
Too short
The next version builds on this one.
Approach
The length is used twice: it is printed, and it is compared with 8. So the program stores it in a variable once and uses the variable both times. Then a single if/else chooses between the two verdicts.
The boundary is the part to get right. "At least 8" includes 8, so the test is length >= 8. A password of exactly 8 characters is accepted, and one of 7 is not.
Step by step
- Store the length. Create an
intnamedlengthand assign itpassword.length(). - Print the first line. Join the text
Password length:andlengthwith+, and pass the result toSystem.out.println. - Write the
if. Whenlength >= 8, printOK. - Add the
else. It runs for every other length, that is 7 or fewer, and printsToo short.
The solution in Java
String password = "Secret99";
int length = password.length();
System.out.println("Password length: " + length);
if (length >= 8) {
System.out.println("OK");
} else {
System.out.println("Too short");
}
The Java details worth a closer look:
length()has parentheses. On aString,lengthis a method, so you writepassword.length(). On an array it is a field without parentheses,array.length. Mixing them up is a compile error.- The type of
lengthisint.length()returns anint, so the variable has the same type. - Joining text and a number. When one side of
+is aString, Java converts the other side to text. That turns"Password length: " + lengthintoPassword length: 8. - Braces and
else. The lesson writes} else {on one line, which is the common Java style. - What the length counts.
length()counts UTF-16 code units. For ordinary letters, digits and punctuation that equals the number of characters. A character outside the basic range, such as an emoji, takes two units:"a😀".length()is 3.
Edge cases and common mistakes
- Exactly 8 characters.
abcdefghprintsPassword length: 8andOK. Writinglength > 8would reject it. - 7 characters.
abcdefgprintsPassword length: 7andToo short. - Empty password.
""has length 0, so the output isPassword length: 0andToo short. Anullpassword is different: callinglength()on it throws aNullPointerException. - Spaces count. A space is a character like any other, so it is included in the length.
- Printing the verdict on the same line. The lesson wants two separate lines, so there are two
printlncalls, not one that joins everything. - Repeating the condition by hand. A second
if (length < 8)gives the same output here, butelseguarantees that exactly one of the two branches runs, with no gap or overlap between them.
Testing
Secret99givesPassword length: 8andOK.abcgivesPassword length: 3andToo short.abcdefghgivesPassword length: 8andOK.abcdefggivesPassword length: 7andToo short.- The empty string gives
Password length: 0andToo short.
