MIS 120 – Homework #5
This assignment is intended to give you a chance to exercise the following programming skills:
• Exception Handling
• Exception Generation (Throwing)
• Basic Text File Input
• Basic Text File Output
We are only creating a single class for this assignment. We will not be building any objects of this
class – all methods we will use will be static. However, this does not mean we have entirely ignored
objects; rather, we are managing and manipulating objects to accomplish our goal.
The intention of the program overall is to read in a text file filled with numbers, one on its own line.
The computer reads these numbers, computes the average of these numbers, and then writes out the
result to a second file – that is, if the file does not exist. It refuses to produce output if the file already
NOTE: ALL METHODS FOR THIS PROGRAM ARE STATIC. We are not instantiating any class
that we wrote here; everything is going to be called directly from the main() function.
There are two constants defined for this program: FILENAME_INPUT and FILENAME_OUTPUT.
Both are String objects.
FILENAME_INPUT is the data we will read to make our calculations. It is a standard text file. You
can create it with Notepad. What numbers it contains are not all that important, just that they are
FILENAME_OUTPUT is the data that we are writing out the results to. It will be a standard text file
with a single number written to it.
FILENAME_INPUT should be set to read “numberdata.txt”, and FILENAME_OUTPUT should read
averageFile() returns a Double. Note that this is a BOXED Double, because if we run into a problem
our program will return a null.
The function should use a File object with the FILENAME_INPUT String describing the precise file
we want to use.
Once you have done this, we use try-with-resources to create a Scanner object. Scanner should take the
File as input.
To come up with a useful calculation we will require three variables: a double to hold the number we
read from the file (currentNumber), another double to hold our current total (currentTotal), and an int
that keeps track of how many numbers we have read from the file (counter).
Within the try block, we must first set currentTotal to 0.0, and counter to 0. We then use a loop, which
uses the Scanner’s hasNextDouble() method to determine if we have another number waiting for us. If
we have another number waiting for us, then we read that number into the currentNumber variable. We
add currentNumber to currentTotal, and then increment the counter. If Scanner’s hasNextDouble()
returns false, we end the loop.
Now at this point we should have read in all the numbers, totaled up in totalNumber, and a count of
how many numbers we read, which we are tracking in the counter variable. We return the result of
totalNumber divided by counter.
However, ALL of this is in the ‘try’ block. Every ‘try’ block needs one or more corresponding ‘catch’
blocks, so that is what we need to write next.
You should check for a FileNotFoundException; if this is caught, print out an error message describing
this, and return null.
You should then check for an Exception; if this is caught, print out an error message describing that an
unknown error occurred, and return null.
writeAverage() accepts a single parameter, a double, which is the average that was computed for us in
averageFile(). It also throws an exception. We are not using a specialized class of exception, just the
general Exception class.
writeAverage() should start by creating a File object, using the FILENAME_OUTPUT String.
It should then use the File object’s exists() method to determine if the file exists.
If the file exists, then we don’t want to overwrite the file with new data, which is what will happen if
we proceed. If this is the case, then we throw an Exception object. The Exception object should have a
message noting that the file already exists and the program is not willing to overwrite it. Throwing an
exception instantly exits the function.
If we have not thrown an exception, everything should be alright.
We use try-with-resources once again, except this time with a PrintWriter.
The try block should use the PrintWriter’s println() method to print out the number that was passed as a
parameter to the writeAverage() function.
The try block should check for an IOException object. If this is caught, then an error message should
be printed out noting an I/O error.
The try block should also check for an Exception object. If this is caught, then an error message should
be printed out noting that we encountered and unknown error.
Regardless of whether or not we caught any exceptions at this point, we exit the method.
main() should be declared in the usual way. We will be using it to manage the use of the other two
methods we wrote.
It should declare one variable, a Double (computedAverage). This Double is a boxed double, because
we may have to process a null value.
First, we run averageFile(), and use the assignment operator to put the value that this method returns
If computedAverage is null, then print out an error message noting that we could not process the
average. The program would do nothing else and can exit, either by using System.exit() or just running
to the end of main() without running any other code, as per usual.
If it is NOT null, however, we can proceed. We build a try block. In the ‘try’ block we have a single
method call, where we call writeAverage(), and provide computedAverage as the parameter.
In the catch block, we check for an Exception object. If this is caught, then we print out an error
message that is comprised of the Exception object’s message. This can be obtained by using the
getMessage() method on the Exception object we received.
Whether or not an exception was caught, we have nothing more to do, so we exit the program at this
TIPS AND POINTERS:
• You will need to write a text file, numberdata.txt. Put several numbers in it, one on each line.
You should compute the average yourself before you begin testing so that you know if the
program produced a correct answer. Make sure it has at least three or four.
• Your program will be making a text file, averagedata.txt. However, you will have to delete this
after every successful run, because the program refuses to write out to this file if it already
exists. Remember – writeAverage() is supposed to throw an exception if the file exists, so it
won’t overwrite the old file! Since it should only contain an average of the elements in
numberdata.txt, this should be no great loss, but it’s still something you need to watch out for.
• For anyone who may be using an IDE, look for the “Working Directory” setting for your
project. This should be where you put numberdata.txt, and where your program will place
• If you are running the JVM from the command line, your “Working Directory” will usually be
the directory that you’re launching it from, so put the files there.
• Remember to look at previous examples that demonstrate the usage of files and exceptions. In
particular, pay attention to how to use the File object, how to throw exceptions, how to catch
exceptions, and how to read and write with files.
• When the program has succeeded, there does not have to be anything printed out to the user.
However, if you wish, you can add your own messages to report the current status of the
program as it works. In fact, if you are having trouble, this is a recommended technique to
assist you in tracking down bugs.
Turn in the source code (.java) for the program in a .ZIP file by the due date.