Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- {
- Scanner fileScan, lineScan;
- String fileName;
- int oCount=0, hCount=0, sCount=0, wCount=0;
- 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())
- {
- fileName=fileScan.nextLine();
- //System.out.println (" " +fileName);
- lineScan = new Scanner (fileName);
- lineScan.useDelimiter(",");
- for (int i=0; i< fileName.length(); i++)
- {
- if (fileName.charAt(i) == 's')
- {
- sCount++;
- }
- else
- if (fileName.charAt(i) == 'o')
- {
- oCount++;
- }
- else
- if(fileName.charAt(i) == 'h')
- {
- hCount++;
- }
- else
- if (fileName.charAt(i) == 'w')
- {
- wCount++;
- }
- }
- System.out.println(lineScan.next()+": Walks: "+wCount+", Hits:"
- + " "+hCount+", Sacrifice: "+sCount+", Outs: "+oCount+", "
- + "Batting average: "+((double)hCount/(double)(hCount+oCount)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement