CISC 3115
Introduction to Modern Programming Techniques
Lab #4
Exception Handling

How to Develop and Submit your Labs

Recall, there are three elements to exception handling: detection, notification, and processing/handling:

Lab 4.1 — A Clever Use of Scanner (LineScanner)

If you look at the API for Scanner, you will see a constructor that accepts a String as it's argument. The semantics of this constructor is that the passed string is treated like an input source upon which you can then call the various Scanner methods: next, nextInt, etc. Thus, for example, if one had the file:
1 2 3
4
5 6 7
one could read in a line at a time, and then using each line as an input source, you could then process the integers on each line.

The basic code structure of this technique is:

…
Scanner fileScanner = new Scanner(new File(filename);

while (fileScanner.hasNextLine() {				// while there are lines left to read
	String line = fileScanner.nextLine();		// read next line of file
	Scanner lineScanner = new Scanner(line);	// set up a Scanner to read the 'contents of the line'
	…
	// use lineScanner just like any other Scanner object
	// i.e., you can call all the hasNext and next  methods
	…
}
This technique can be useful when one want to limit their processing of data to that on a single line of a file. Using next or nextInt doesn't work because those methods treat newlines like blanks (i.e., all whitespace is treated in the same fashion), so one doesn't know when the end of the line has been reached. While there are several ways of handling this, the above-described technique of reading in a line at a time — using nextLine and then creating a new Scanner object from the resulting string returned is a fairly straightforward way of accomplishing this.

Write an application class, named LineScanner, that opens the file numbers.text and prints out the number of integers on each line of the file. Again, to accomplish this, read each line into a String using nextLine and then create a second Scanner object using that string as the constructor's argument.

Sample Test Run

For example if the file numbers.text contains (notice the blank third line):

1 2 3
5 10 15 20 25

10 9 8 7 6 5 4 3 2 1
the program should produce the following output:
There are 3 numbers on line 1
There are 5 numbers on line 2
There are 0 numbers on line 3
There are 10 numbers on line 4

Some Guidance and Notes

Submitting to CodeLab

No special restrictions (i.e., no stripping of public necessary).

Lab 4.2 — Data Validation (DataChecker)

This lab builds on the technique presented in Lab 4.1, and uses it to validate structured data in a file.

The file numbers.text contains data in the following format:

header1 int1 int2header2 int1 int2 …
…
i.e., each line begins with a header value, followed by that number of integers. Your program's task is to print out the average of each of these sequences.

However, the file contents is often corrupted by the time it gets to your program, so the data must be validated. Here are the possible problems:

Each of these situations must be detected and if present, an exception should be thrown, no average is calculated, and processing proceeds to the next sequence.

Sample Test Run

For example if the file numbers.text contains:

3 1 2 3

0
1 12
-12
14 1 2 3 
5 1 2 3 4 5 6
4 10 20 30 40
the program should produce the following output:
The average of the values on line 1 is 2.0
*** Error (line 2): Line is empty - average can't be taken
*** Error (line 3): Header value of 0 - average can't be taken
The average of the values on line 4 is 12.0
*** Error (line 5): Corrupt line - negative header value
*** Error (line 6): Corrupt line - fewer values than header
*** Error (line 7): Corrupt line - extra values on line
The average of the values on line 8 is 25.0

There were 3 valid lines of data
There were 5 corrupt lines of data

Some Guidance and Notes

Submitting to CodeLab

No special restrictions (i.e., no stripping of public necessary).

Lab 4.3 — A User Class (User)

Implement the following User class: Sample Test Run

In CodeLab, I will be supplying a UserApp class to test your class by reading in Users from a file using your read method, print out the User using your toString, and makes sure the password is not the same as the username by calling verifyPassword passing it the username.

For example if the file users.data contains:

weiss	puppy2	woof-woof
arnow	java		cuppa
tenenbaum tenenbaum	da-same
sokol	brooklyn	college
the program (i.e., UserApp) should produce the following output:
User weiss hint: woof-woof
OK -- the password is different than the user name

User arnow hint: cuppa
OK -- the password is different than the user name

User tenenbaum hint: da-same
*** Error the password should not be the same as the user name

User sokol hint: college
OK -- the password is different than the user name

Some Notes

Submitting to CodeLab

Lab 4.4 — An Authenticator Class (Authenticator)

Implement the following class name Authenticator: Again — as in the previous lab — in CodeLab, I will be supplying an application class testing the Authenticator class by supplying it with a user data file, and then a sample login session. The login session consists of:

Sample Test Run #1

For example if the file users.data contains:

weiss	puppy2	woof-woof
arnow	java		cuppa
sokol	brooklyn	college
execution of the program should look like:
username? arnow
password? java
Welcome to the system

Sample Test Run #2

Given the same users.data file as above, execution of the program should look like:

username? weiss
password? dontremember
*** Invalid password - hint: woof-woof
username? weiss
password? puppy2
Welcome to the system

Sample Test Run #3

Given the same users.data file as above, execution of the program should look like:

username? sokol
password? CUNY
*** Invalid password - hint: college
username? sokol
password? SUNY
*** Invalid password - hint: college
username? sokol
password? BC
*** Invalid password - hint: college
Too many failed attempts... please try again later

Some Guidance and Notes

Submitting to CodeLab

  • More exception-handling (actually exception 'throwing') practice
  • A glimpse into a 'real-life' application
  • Lab 4.5 — An Authenticator Application (AuthenticatorApp)

    Assuming the existence of the Authenticator class of Lab 4.4, code an AuthenticatorApp application that reproduces the behavior of the sample runs of Lab 4.4, i.e., your app should instantiate an Authenticator object passing it the filename users.data, and then proceed to prompt the user at the keyboard for a user name and password; handling any exceptions in the manner described in Lab 4.4.
  • Exception-handling practice — this app catches the exceptions thrown by the Authenticator class.
  • The 'other side of the coin' if Lab 4.4 — there you write the class; here you write an app using the class.
  • Submitting to CodeLab