CS 151 Fall 2007 Lab 12
Reading
Input, Writing Output, Handling Exceptions
The goals for this lab are to get more practice reading from
and writing to files and handling errors.
- Open BlueJ
either by selecting it from the Dock or navigating to it in the
Applications folder and double clicking it.
- Mount your network
directory/folder so that you can access the lab after you leave Olin
323. Go to the Finder and
from the
Go menu select Connect to Server.... Type afp://fileserver1 or choose it from the
list of selections and select the Personal volume
when prompted.
- From
the Project menu select New Project. Select your computer from the drop
down menu. Then scroll down
and open the Volumes folder and then the Personal folder inside it. Scroll down the long list of
folders until your find yours.
Create a project called "Lab12" in your personal network
folder.
- (Exercise P11.1 from
your book) Write a program that reads a specified file and prints out the
number of lines, words, and characters in it. Call the class FileStats or some other meaningful
name. Assume that the user
specifies the file name when calling the main method of your class. This means you can find it in the
main method's parameter args (a String array). Don't forget to import the Scanner class from the
java.util package and FileReader and whatever else you need from the
java.io package (java.io.*).
For starters, just throw the checked FileNotFound exception that
might occur when you try to open the file. Recall that for a given String, calling its split()
method with a blank space, " ", as a parameter returns an array
of words. Create your own
file(s) for testing.
- Modify your program so
that if args doesn't contain a file name or the one it contains can't be
found, the user is prompted for a filename. If the file name the user
enters on the command line can't be found, keep prompting the user until
he or she enters a valid file name.
Recall that to get terminal input from the user you need to create
a Scanner object connected to System.in. First use an if clause on the length of args to test
whether or not it contains a name.
Then, instead try catching the ArrayIndexOutOfBoundsException thrown
when access args[0] when args is empty (not null, just empty).
- (Based on Exercise
P11.14 from your book) Time permitting, write another program that reads a
list of PlayingCards from a file and creates a BlackJackDeck containing
those cards. The input file
has the format:
suit1 rank1 value1
suit2 rank2 value2
...
where
suit1 and rank1 are strings and value1 is an integer. Feel free to use your Project 5 PlayingCard and BlackJackDeck
classes or the hyperlinked versions from my solution. This should give you some ideas
for the restore method for your project. If you don't get to this in lab, spend your time on
your project instead.
- Add a method
void read(Scanner input)
throws IOException
to the PlayingCard class.
Note that essentially all it does is read in the suit, rank, and
value for the card from which it's called. Throw an exception if the current line is not properly
formatted.
- Add to the
BlackJackDeck class a method
static
ArrayList<PlayingCard> readFile(String filename) throws IOException
It should create cards, read their information from the file using read()
and add them to the ArrayList.
Also add another BlackJackDeck constructor that takes the name of a
file as a parameter. It
should call readFile() and construct a deck with the ArrayList that
readFile() returns. What
should the constructor do with any exceptions that readFile() might throw?