Exercise 4: String Handling & The Command Line

Copy the file CheckPassword.java to the directory exercises/ex4 of your repository. 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>

Make sure that your program generates exactly the same output as these examples.

Go to Submit My WorkExercises 1-5Exercise 4 in Minerva to submit your solution for grading. Also, don’t forget to commit your work to your Git repository and push those commits up to GitLab.

Tips