ITEC 120 LAB 17  

Baseball Statistics

The local Kids' League coach keeps some of the baseball team statistics in a text file organized as follows: each line of the file contains the name of the player followed by a list of symbols indicating what happened on each at bat for the player. The letter h indicates a hit, o an out, w a walk, and s a sacrifice fly. Each item on the line is separated by a comma. There are no blank spaces except in the player name. So, for example the file could look as follows:

Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s
Jill Jenks,o,o,s,h,h,o,o
Will Jones,o,o,w,h,o,o,o,o,w,o,o

The file BaseballStats.java contains the skeleton of a program thats reads and processes a file in this format. Study the program and note that three Scanner objects are declared.

Also note that the main method throws an IOException. This is needed in case there is a problem opening the file. Exceptions are discussed in detail in Chapter 10.

Complete the program as follows:

  1. First add a while loop that reads each line in the file and prints out each part (name, then each at bat, without the commas) in a way similar to the URLDissector program in Listing 5.11 of the text. In particular inside the loop you need to
  2. Compile and run the program to be sure it works.
  3. Now modify the inner loop that parses a line in the file so that instead of printing each part it counts (separately) the number of hits, outs, walks, and sacrifices. Each of these summary statistics, as well as the batting average, should be printed for each player. Recall that the batting average is the number of hits divided by the total number of hits and outs.
  4. Test the program on the file stats.dat and stats2.dat .