Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

First add a while loop that reads each line in the file and prints out each part

ID: 3681431 • Letter: F

Question

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 file stats.dat and stats2.dat

. // **************************************************************** // BaseballStats.java // //

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; import java.io.*;

// ****************************************************************

// BaseballStats.java // // 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; import java.io.*; 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 76

{ Scanner fileScan, lineScan; String fileName; Scanner scan = new Scanner(System.in); 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

Explanation / Answer

import java.util.Scanner;
import java.io.*;

/**
* @author Srinivas Palli
*
*/
public class BaseballStats {

   /**
   * @param args
   * @throws IOException
   */
   public static void main(String[] args) throws IOException {
       int hit, out, walk, sacrifice;
       double average;
       Scanner fileScan, lineScan;

       String fileName;
       String current;

       Scanner scan = new Scanner(System.in);
       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
       while (fileScan.hasNext()) {
           hit = 0;
           out = 0;
           walk = 0;
           sacrifice = 0;
           String player = fileScan.nextLine();
           System.out.println("Player: " + player);

           lineScan = new Scanner(player);
           lineScan.useDelimiter(",");

           // Print each part of the url
           while (lineScan.hasNext()) {
               current = lineScan.next();
               if (current.equals("h"))
                   hit++;
               if (current.equals("o"))
                   out++;
               if (current.equals("w"))
                   walk++;
               if (current.equals("s"))
                   sacrifice++;
               System.out.print(" " + current);

           }
           average = (double) hit / (double) (hit + out);
           System.out.println(" Hits:" + hit + " Outs:" + out
                   + " Walks:" + walk + " Sacrifice:" + sacrifice);
           System.out.printf(" Batting Average:%.2f ", average);

       }

   }
}

stats.data

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

OUTPUT:

Enter the name of the input file: stats.dat
Player: Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h
Willy Wonk o o h o o o o h w o o o o s h o h
   Hits:4
   Outs:11
   Walks:1
   Sacrifice:1
   Batting Average:0.27
Player: Shari Jones,h,o,o,s,s,h,o,o,o,h,o,o,o,o
Shari Jones h o o s s h o o o h o o o o
   Hits:3
   Outs:9
   Walks:0
   Sacrifice:2
   Batting Average:0.25
Player: Barry Bands,h,h,w,o,o,o,w,h,o,o,h,h,o,o,w,w,w,h,o,o
Barry Bands h h w o o o w h o o h h o o w w w h o o
   Hits:6
   Outs:9
   Walks:5
   Sacrifice:0
   Batting Average:0.40
Player: Sally Slugger,o,h,h,o,o,h,h,w
Sally Slugger o h h o o h h w
   Hits:4
   Outs:3
   Walks:1
   Sacrifice:0
   Batting Average:0.57
Player: Missy Lots,o,o,s,o,o,w,o,o,o
Missy Lots o o s o o w o o o
   Hits:0
   Outs:7
   Walks:1
   Sacrifice:1
   Batting Average:0.00
Player: Joe Jones,o,h,o,o,o,o,h,h,o,o,o,o,w,o,o,o,h,o,h,h
Joe Jones o h o o o o h h o o o o w o o o h o h h
   Hits:6
   Outs:13
   Walks:1
   Sacrifice:0
   Batting Average:0.32
Player: Larry Loop,w,s,o,o,o,h,o,o,h,s,o,o,o,h,h
Larry Loop w s o o o h o o h s o o o h h
   Hits:4
   Outs:8
   Walks:1
   Sacrifice:2
   Batting Average:0.33
Player: Sarah Swift,o,o,o,o,h,h,w,o,o,o
Sarah Swift o o o o h h w o o o
   Hits:2
   Outs:7
   Walks:1
   Sacrifice:0
   Batting Average:0.22
Player: Bill Bird,h,o,h,o,h,w,o,o,o,h,s,s,h,o,o,o,o,o,o
Bill Bird h o h o h w o o o h s s h o o o o o o
   Hits:5
   Outs:11
   Walks:1
   Sacrifice:2
   Batting Average:0.31
Player: Don Daring,o,o,h,h,o,o,h,o,h,o,o,o,o,o,o,h
Don Daring o o h h o o h o h o o o o o o h
   Hits:5
   Outs:11
   Walks:0
   Sacrifice:0
   Batting Average:0.31
Player: Jill Jet,o,s,s,h,o,o,h,h,o,o,o,h,o,h,w,o,o,h,h,o
Jill Jet o s s h o o h h o o o h o h w o o h h o
   Hits:7
   Outs:10
   Walks:1
   Sacrifice:2
   Batting Average:0.41