THINK FIRST·CODE LATER

← All labs

Password rules, one method per rule

Problem

A site accepts a password only if it (1) has at least 8 characters, (2) contains only letters and digits, (3) contains at least 2 digits and (4) contains a capital letter.

Write PasswordCheck with one method per rule — longEnough, onlyLettersAndDigits, countDigits, hasCapital — plus isValid, which combines them, and problems, which returns the list of broken rules. main reads passwords until the end of the input. For the input

Campus2026 abc Wenzhou1 pass_word99 NoDigitsHere 12345678 Ab12cd34

the program prints

Campus2026: valid
abc: invalid - fewer than 8 characters; needs at least 2 digits; needs a capital letter;
Wenzhou1: invalid - needs at least 2 digits;
pass_word99: invalid - only letters and digits are allowed; needs a capital letter;
NoDigitsHere: invalid - needs at least 2 digits;
12345678: invalid - needs a capital letter;
Ab12cd34: valid

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l8-6-data-1.txt

Campus2026 abc Wenzhou1 pass_word99 NoDigitsHere 12345678 Ab12cd34
What your program must print for l8-6-data-1.txt
Campus2026: valid
abc: invalid - fewer than 8 characters; needs at least 2 digits; needs a capital letter;
Wenzhou1: invalid - needs at least 2 digits;
pass_word99: invalid - only letters and digits are allowed; needs a capital letter;
NoDigitsHere: invalid - needs at least 2 digits;
12345678: invalid - needs a capital letter;
Ab12cd34: valid

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l8-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l8-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.