BaseballStat. java to call the file and print output use the below link to download file to call...
Click Here
//************************************************************************************
// BaseballStat.java
// To call the file and print output
//************************************************************************************
import java.util.Scanner;
import java.io.*;
public class BaseballStat
{
public static void main (String[] args) throws IOException
{
int hit, out, walk, sacrifice;
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));
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(",");
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.println("Hits: " + hit + " Outs: " + out + " Walks: " + walk + " Sacrifice: " +sacrifice);
System.out.println("");
}
}
}
No comments:
Post a Comment