and File I/O
Aren't Always Errors
contains a program that reads a word from the user and prints the
number of occurrences of each letter in the word. Save it to your
directory and study it, then compile and run it to see how it works. In
code, note that the word is converted to all upper case first, then
each letter is
translated to a number in the range 0..25 (by subtracting
'A') for use as an index. No test is done to ensure that the characters
are in fact letters.
- Run CountLetters
and enter a phrase, that is, more than one word with spaces or other
punctuation in between. At the end of the first word, this program
will throw an ArrayIndexOutOfBoundsException,
because a non-letter will generate an index that is not between 0 and
25. Of course, you could explicitly test the value of the
character to see if it is between 'A' and 'Z'. However, an alternative
is to treat
the translated character as an index, and catch the ArrayIndexOutOfBoundsException
when it occurs. When a non-letter appears, it is effectively the end of
the word; you want to ignore the rest of the phrase and display
the count. Since your only response to a non-letter is to stop
processing, the handler will be empty. Modify this method to do this as
Now modify the body of the catch so that it prints a useful
message (e.g., "Not a letter") followed by the exception message
Remember that an exception is an object (which happens to have a
toString method!) Compile and run the program.
- Put the body of the first for loop in a try.
- Add a catch that catches the exception, but don't do anything
- Compile and run your program
it's useful to print the
exception for debugging, when you're trying to smoothly handle a
you don't consider erroneous you often don't want to display the
exception explicitly. Notice that when the exception was printed it
generally what the exception was and then there was a more specific
message(i.e.which index that was bad). You can often extract the
specifics of an
exception using a method from the exception object. Look at the
online Java documentation for
IndexOutOfBoundsException to see how to extract these details.
this class inherits its interesting methods from its superclasses, so
you might need to look at some of those to find a useful method) Use
this information to change the 2nd
print statement to only display the character that
created the out of bounds
Print a copy of CountLetters.java to turn in.
Placing Exception Handlers
contains a program that does the following:
Save ParseInts to your directory, compile and run it. If you give it
- Prompts for and reads in a line of input
- Uses a second Scanner to take the input line one token at a time
and parses an integer from each token as it is extracted.
- Sums the integers
- Prints the sum.
10 20 30 40
It should print
The sum of the
integers on the line is 100.
Now try a line that contains both
integers and other values, e.g:
We have 2 dogs
and 1 cat.
You will get a NumberFormatException
when it tries to call Integer.parseInt
on "We", which is not an integer. One way around this is to put the
loop that reads inside a try and catch
the NumberFormatException but not do
it. This way if it's not an integer it doesn't cause an error; it goes
exception handler, which does nothing. Do this as follows:
Print a copy of ParseInts.java to turn in.
- Modify the program to add a try statement that encompasses the
entire while loop. The try and
opening should go before the while,
and the catch after the loop body.
Catch a NumberFormatException and have an
empty body for the catch.
and run the program and enter a line with mixed integers
and other values. You should find that it stops summing at the first
non-integer, so the line above will produce a sum of 0, and the line "1
fish 2 fish" will produce a sum of 1. This is because the entire loop
inside the try, so when an exception
is thrown the loop is terminated. To make it continue, move the try and catch inside the loop. Now when
an exception is thrown, the next
statement is the next iteration of the loop, so the entire line is
The dogs-and-cats input should now give a sum of 3, as should the fish
contains a program that calls the factorial
method of the MathUtils.java
class to compute the factorials of integers entered by the user. Save
these files to your directory and study the code in both, then compile
run Factorials to see how it works. Try several positive integers, then
try a negative number. You should find that
it works for small positive integers (values < 17), but that it
large negative value for larger integers and that it always returns 1
negative integers. Returning 1 as the factorial of any negative integer
is not correct mathematically, the factorial function is not defined
for negative integers. To correct this, you could modify your factorial
method to check if the
argument is negative, but then what? The method must return a value,
and even if it prints an error message, whatever value is returned
misconstrued. Instead it should throw an exception indicating that
something went wrong so it could not complete its calculation. You
your own exception class, but there is already an exception appropriate
situation IllegalArgumentException, which
extends RuntimeException. Modify your
program as follows:
Returning a negative number for values over 16 also is not correct. The
problem is arithmetic overflow the factorial is
bigger than can be represented by an int. This can also be thought of
as an IllegalArgumentException this factorial
method is only defined for arguments up to 16.
Modify your code in factorial to check for
an argument over 16 as well as for a negative argument. You should
throw an IllegalArgumentException in
either case, but pass different
messages to the constructor so that the problem is clear.
- Modify the header of the factorial method to
indicate that factorial can throw an
- Modify the body of factorial
to check the value of the argument and, if it is negative, throw an
IllegalArgumentException. Note that what you
pass to throw is actually an instance of the
IllegalArgumentException class, and that the
takes a String parameter. Use this parameter to be specific about what
the problem is.
- Compile and run your Factorials program after making these
changes. Now when you enter a negative number an exception will be
thrown, terminating the program. The program ends because the exception
caught, so it is thrown by the main method, causing a runtime error.
- Modify the main method in your Factorials class to catch the
exception thrown by factorial and print an appropriate message, but
then continue with the loop. The message should include only the
passed with the exception, not the exception itself!
Print a copy of Factorial.java and MathUtils.java to turn in.
Reading a User-Selected File
Write a program that prompts the user for a filename, then opens a
Scanner to the file and copies it, a line at a time, to the standard
output. If the user enters the name of a
file that does not exist, ask for another name until you get one that
refers to a valid file. Some things to consider:
Print a copy of your program to turn in.
that you can create a Scanner from a File object, which you can create
from the String representing the filename.
- The Scanner constructor that takes a File may throw a
-- this is how you will know if the file does not exist. Think
carefully about how to use the try/catch structure in combination with
a loop that asks for a new filename if the current file does not exist.
- Remember that the scope of a variable declared inside a try is
the try itself --
it does not extend to the following code. Furthermore, the compiler
knows that an initialization that occurs inside a try may or may not
get executed, as the try may be thrown out of first. So
any variable that you will want to use both in and after the try must
be declared and initialized before the try.
from and Writing to Text Files
Write a program that will read in a file of student academic
credit data and create a list of students on academic warning. The list
of students on warning will be written to a file. Each line of the
file will contain the student name (a single String with no spaces),
of semester hours earned (an integer), the total quality points earned
double). The following shows part of a typical data file:
Smith 27 83.7
Jones 21 28.35
Walker 96 182.4
Doe 60 150
The program should compute the GPA (grade point or quality point
for each student (the total quality points divided by the number of
semester hours) then write the student information to the output file
student should be put on academic warning. A student will be on warning
he/she has a GPA less than 1.5 for students with fewer than 30 semester
credit, 1.75 for students with fewer than 60 semester hours credit, and
2.0 for all other students.
The file Warning.java
contains a skeleton of the program. Do the
Print a copy of your program to turn in.
- Set up a Scanner object scan from
the input file and a PrintWriter outFile to the
output file inside the try clause (see the
comments in the program). Note that you ll have to create the
PrintWriter from a FileWriter,
but you can still do it in a single statement.
- Inside the while loop add code to read and parse
the input get the name, the number of credit hours, and the number of
quality points. Compute the GPA, determine if the student is on
warning, and if so write the name, credit hours, and GPA (separated by
the output file.
- After the loop close the PrintWriter
- Think about the exceptions that could be thrown by this program:
- A FileNotFoundException if the input file does not exist
- An InputMismatchException if it can't parse an int or double
when it tries
to. This indicates an error in the input file format
- An IOException if something else goes
wrong with the input or output stream
Add a catch for each of these
situations, and in each case give as specific a message as you can. The
program will terminate if any of these exceptions is thrown, but at
least you can supply the user with useful
- Test the program. Test data is in the file a
students.dat Be sure to test each
of the exceptions as well.
- Turn in stapled hardcopies of your code
- Tar your directory and email to me with cpsc170 lab9 in
the Subject line.