BaseballStats

BaseballStats - while (fileScan.hasNext()) { line =...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// **************************************************************** // 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.*; import java.text.DecimalFormat; public class BaseballStats { public static void main (String[] args) throws IOException { Scanner fileScan, lineScan; String fileName; String line; //a line from the file String playerName; int atBats, numHits, numWalks, numSacrifices, numOuts; String action; 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: while (fileScan.hasNext()) { line = fileScan.nextLine(); lineScan = new Scanner(line); lineScan.useDelimiter(","); atBats = 0; numHits = 0; numOuts = 0; numSacrifices = 0; numWalks = 0; playerName = lineScan.next(); while (lineScan.hasNext()) { action = lineScan.next(); if (action.equals("h")) numHits++; else if (action.equals("o")) numOuts++; else if (action.equals("s")) numSacrifices++; else if (action.equals("w")) numWalks++; } //print statistics for the player System.out.println ("\nStatistics for " + playerName + ". .. "); System.out.println ("Hits: " + numHits); System.out.println ("Outs: " + numOuts); System.out.println ("Walks: " + numWalks); System.out.println ("Sacrifices: " + numSacrifices); atBats = numOuts + numHits; DecimalFormat fmt = new DecimalFormat(".000"); if (atBats == 0) System.out.println ("No at bats"); else System.out.println ("Batting Average: " + fmt.format((double)numHits/atBats)); } } }...
View Full Document

This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.

Page1 / 2

BaseballStats - while (fileScan.hasNext()) { line =...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online