Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The local Kids' League coach keeps some of the baseball team statistics in a text file organized as follows: each line of the file

image text in transcribed imageimage

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, 0,0 Will Jones, o, o,w,h,0,0,0,0,0,0,0 The file BaseballStats.java contains the skeleton of a program that 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 FileNotFoundException. This is needed in case there is a problem opening the file. 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.10 of the text. In particular inside the loop you need to a. read the next line from the file b. create a comma delimited scanner (lineScan) to parse the line c. read and print the name of the player, and finally, d. have a loop that prints each at bat code. 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 files stats.dat and stats2.dat. import java.util.Scanner; * 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. * @author blink * */ 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 FileNot FoundException Scanner fileScan; Scanner lineScan; String fileName; Scanner scan = new Scanner (System.in); //TODO Read and process each line of the file System.out.print ("Enter the name of the input file: "); fileName = scan.nextLine(); fileScan = new Scanner (new File (fileName));

Step by Step Solution

There are 3 Steps involved in it

Step: 1

import javaioFile import javaioFileNotFoundException import javautilScanner public class BaseballSta... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Modeling and Database Design

Authors: Narayan S. Umanath, Richard W. Scammel

2nd edition

1285085256, 978-1285085258

More Books

Students also viewed these Programming questions