CMPU 101 - Lab 11

In this week's lab you will start working on Assignment 8.  Specifically, you should write code that will the text file containing baby names (babynames.txt) and, for each line of the file, extract each data item.

Suggested Approach

Create a class (suggested name: BabyNameTabulator) that contains a single instance method called readFile.  The method should

  1. Be public
  2. Have a void return type
  3. Take a String as a parameter, representing the filename of the file to read
  4. Be declared to throw java.io.IOException

In this method, use FileReaderBufferedReader to read each line of the file using BufferedReader's readLine method.  Use a Scanner to decode the data items contained in each line of the file.  (See the assignment description for details.)

Running Your Program

You can use a shortened text file containing only the first 10 lines of the full data file: babynames-first10.txt.

It may be easier to run your program from a terminal window rather than the DrJava interactions window.  To do so, open a terminal window, and change directory to whatever directory your Java source files are in.  If your Java files are in the Home directory, type the command

cd

in the terminal window.  If your source files are on the Desktop, type the commands

cd
cd Desktop

Once you are in the directory where your source files are located, run your program with the command

java MyClassName

Where MyClassName is the name of the class containing your main method.  For this lab, your main method can be very simple: for example:

public static void main(String[] args) throws IOException {
  Scanner keyboard = new Scanner(System.in);
  System.out.print("Name of baby names file: ");
  String fileName = keyboard.next();

  BabyNameTabulator tabulator = new BabyNameTabulator();
  tabulator.readFile(fileName);
}

This main method can serve as a starting point for your full implementation of Assignment 8.

When You Are Done

Show your file-reading code to an instructor or lab coach.  Demonstrate that it can read and decode each line of the shortened data file.  For example, your program should be capable of printing out each data item (decade, rank, sex, name, and count) separately for each line.

You do not need to submit any files.  However, you will almost certainly want to save your code in order to use it as part of your implementation of Assignment 8.