Lab 8: Exceptions

Due 21/22 Mar

We are going to write a program that reads blocks of text from the command-line and determines whether they can be parsed as ints or floats, or whether they can only be read as strings.

The purpose of this lab is to exercise our newfound knowledge of Exception-handling. So even though there are several ways to solve this problem, your solution must use the Exceptions as described below.

Just for fun, we are going to solve most of this lab using static classes and methods.

The Assignment

You must create a file named 'Lab08.java'. All of your code must be contained in this file.

You must create a public static class named "NotAnIntException". It must inherit from class Exception. This custom Exception must have the same two constructors that were shown in the sample for yesterday's lecture (YouDidSomethingDumbException).

You must create a public static class named "NotAFloatException". It must inherit from class Exception. This custom Exception must have the same two constructors that were shown in the sample for yesterday's lecture.

You must create a public static method named "stringToInteger" that accepts a string as an argument and returns an integer. If the string cannot be parsed to an int, then the method must throw a "NotAnIntException".

You must create a public static method named "stringToFloat" that acts similar to the above, except it deals with floats.

The very first thing your main method must do it to print the 5th command-line argument (args[4]). If there are fewer than 5 arguments, then you must print "none". It would be too easy to check the length of args with an if-then statement, so I want you to use a try/catch block to print this line: (It may be a poor coding-style, but for this bullet you may not reference args.length). See the examples below for the expected output.

The main method in Lab08.java must iterate over a list of command-line arguments and determine which can be parsed as ints, floats, or strings. It must use the stringToInteger and stringToFloat methods, as well as the custom Exceptions that you created to make this determination. (e.g., if you can parse a string to an int, then it is an int. If parsing a String throws a NotAnIntException, then it must be something else.) The code must print out out the results of its finding, as shown in the examples below.

If no arguments are passed, Lab08 should print out a helpful usage message.

Helpful Hint - In Java, think of int->float->String as a hierarchy. An int can also be interpreted as a float or a String, but not every float or String can be interpreted as an int. The same relationship holds for float->String. Your code should pick the most selective interpretation. So "5.0" could be interpreted as either a float or String, but not an int. You should interpret it as a float, since float is more restrictive than String.

Example run

$ java Lab08 34 1 1. 2.34 -2 23q this is a test
FIFTH ARG: -2
INT:    34
INT:    1
FLOAT:  1.0
FLOAT:  2.34
INT:    -2
STRING: 23q
STRING: this
STRING: is
STRING: a
STRING: test


$ java Lab08 1 2 3.0 test 
FIFTH ARG: none
INT:    1
INT:    2
FLOAT:  3.0
STRING: test


$ java Lab08
FIFTH ARG: none
Usage: Pass some arguments on the command line.
       Lab08 will determine if they are INT, FLOAT, or STRING.
       e.g.: java Lab08 1 2 3.0 test 

Fun with command-line parsing

The Linux OS will parse some of the characters on the command-line before Java sees them. This can cause unexpected behavior. It does not mean that your program is broken, it justs means that your code needs to work within the constraints of the OS. It is an important step in your education to recognize when the OS may change the command-line arguments before they get to your program.

Once you get your program working, try running it with these arguments and see if you can understand why the ourput looks like it does.

$ java Lab08 *

$ java Lab08 &^

$ java Lab08 1 2 3 ; echo hello