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: Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s In this problem you will a create a program that reads raw data from a text file and prints properly formatted statistics. For instance, a sample output for the first player in the example: Statistics for Willy Wonk:
Write a Java Program
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:
Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h
Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s
In this problem you will a create a program that reads raw data from a text file and
prints properly formatted statistics. For instance, a sample output for the first
player in the example:
Statistics for Willy Wonk:
Hits: 4
Outs: 11
Walks: 1
Sacrifices: 1
Batting Average: 0.267.
1. Create a text file “stats.txt” containing the data below:
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
3
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
2. In your program, scan through the lines of the file and print each line as is on a
separate Line 3.
In addition to simply printing each line, you will parse each line of the file and
count the number of hits, outs, walks and sacrifices as well as the batting average
(3 digits after the decimal point) which is the number of hits divided by the total
number of hits and outs (Make sure to avoid dividing by zero).
Refer to sample output above
Hint: You will need to create another Scanner that takes as parameter the line
you want to go through. Make sure to use the correct delimiter.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps