The local Kids’ League Baseball coach likes to keep the demanding parents happy by recording some of the team batting statistics for them. He takes very simple notes, recording the result of each individual at-bat by each player on the team. These have been storedin 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 that player. The letter h indicates a hit, o an out, w a walk, and s a sacrifice fly. Every 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 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.

  • One scanner (scan) is used to read in a file name from standard input
  • The file name is then used to create a scanner (fileScan) to operate on that file.
  • A third scanner (lineScan) will be used to parse each line in the file.

Also note that the main method throws an IOException. This is needed in case there is a problem opening the file.

Complete the program as follows:

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.10 of the text. In particular inside the loop you need to

  • read the next line from the file
  • create a comma delimited scanner (lineScan) to parse the line
  • read and print the name of the player
  • have an inner, second loop that prints each at bat code in the line

Compile and run the program to be sure it works.

Now modify the inner loop that parses a line in the file so that instead of printing each part, it counts up (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. The batting average is the number of hits divided by the total number of hits and outs; it will be a value of 1.000 or less, and it should be printed using three decimal places.

Create at least two test files stats1.txt and stats2.txt provided below. Make sure your program gives the correct answers for the sample files.

Program Outline

// ****************************************************************
// Reads baseball data in from a comma delimited file.
// Each line of the file contains a name followed by a list of
// symbols indicating the result of each at bat:
// h for hit, o for out, w for walk, s for sacrifice.
// Statistics are computed and printed for each player
// ****************************************************************
import java.util.Scanner;
public class BaseballStats
// Reads baseball stats from a file and counts
// total hits, outs, walks, and sacrifice flies
// for each player.
public static void main (String[] args) throws IOException
Scanner fileScan, lineScan;
String fileName;
Scanner scan = new Scanner(;
System.out.print ("Enter the name of the input file: ");
fileName = scan.nextLine();
fileScan = new Scanner(new File(fileName));
// Read and process each line of the file
// Print out results


Barry Bands,h,h,w,o,o,o,w,h,o,o,h,h,o,o,w,w,w,h,o,o
Sammy Sub,o,o,o,w,w,o,h,s


Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h
Shari Jones,h,o,o,s,s,h,o,o,o,h,o,o,o,o
Barry Bands,h,h,w,o,o,o,w,h,o,o,h,h,o,o,w,w,w,h,o,o
Sally Slugger,o,h,h,o,o,h,h,w
Missy Lots,o,o,s,o,o,w,o,o,o
Joe Jones,o,h,o,o,o,o,h,h,o,o,o,o,w,o,o,o,h,o,h,h
Larry Loop,w,s,o,o,o,h,o,o,h,s,o,o,o,h,h
Sarah Swift,o,o,o,o,h,h,w,o,o,o
Bill Bird,h,o,h,o,h,w,o,o,o,h,s,s,h,o,o,o,o,o,o
Don Daring,o,o,h,h,o,o,h,o,h,o,o,o,o,o,o,h
Jill Jet,o,s,s,h,o,o,h,h,o,o,o,h,o,h,w,o,o,h,h,o

Example output

Your program does not have to match this exactly, but it should look similar and have successive lines formatted in a way that columns line up vertically.

Input file name: stats1.txt
Player Name AB H W O S AVG
--------------- -- -- -- -- -- ----
Barry Bands 20 6 5 9 0 .400
Sammy Sub 8 1 2 4 1 .200
Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.