Exercise 4: String Handling & The Command Line

Create a directory for this exercise and download the file CheckPassword.java to this directory. This file contains the skeleton of a small program to check the validity of a password. Your task is to add the missing code to this skeleton.

For a password to be deemed valid, it should have a length of at least twelve characters, and it should contain at least two digits. These two checks should be performed by the static methods longEnough() and atLeastTwoDigits(), respectively. Each method should return a value of true if the required condition is satisfied, or false if it is not.

The password to be checked must come from the command line used to invoke the program. You should NOT prompt for input of the password once the program is actually running. Consult the Java Tutorials for an explanation of command line arguments if you are not sure how they work. After checking the password, the program should print a short message (see below), informing the user whether it is valid or not.

If no password has been provided on the command line, the program should print a usage message (see below) to the standard error stream and then terminate with a non-zero exit status code.

Here are examples of expected program behaviour:

$ java CheckPassword jkft45aa
Password is not valid
$ java CheckPassword axhpqwcn99pd
Password is valid
$ java CheckPassword
Usage: java CheckPassword <password>
Tips