0

For this program, we need to read in a file which has baseball stats. Our program reads in the dat file then parses each line in the file. The file looks like this:

Joe h,o,h,s,h,o,h,h,h,o,o,o
Bill s,o,o,h,h,o,s,s,o

So far I'm ok with this. Here is the part that I'm struggling with. We have to modify the inner loop that parses a line in the file to count (seperately) the number of h, o, and s in each line.

Any help would be appreciated.

Here is my java program so far.

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;


Scanner scan = new Scanner(System.in);


System.out.print ("Enter the name of the input file: ");
fileName = scan.nextLine();
fileScan = new Scanner(new File("stats.dat"));


// Read and process each line of the file


while (fileScan.hasNext())
{
fileName=fileScan.nextLine();


lineScan = new Scanner (fileName);
lineScan.useDelimiter(",");
while (lineScan.hasNext())
lineScan.next();
System.out.println();



}
}
}

Edited by Nick Evan: Fixed formatting

3
Contributors
6
Replies
7
Views
11 Years
Discussion Span
Last Post by Phaelax
0

I'm thinking this is how you would do it: Have three counter variables:

sCount
oCount
hCount

String input = file.readLine();

for (int i=0; i<input.length(); i++)
{
  if (input.charAt(i) == "s")
  {
         sCount++;
  }
  else if (input.charAt(i) == "o")
  {
        oCount++;
  }
  else if(input.charAt(i) == "h")
  {
        hCount++;
  }
}

Of course that String is after you deal with parsing the name of the player
Don't know if that will help

0

ok, good so far but how do I parse the names for the players?

0

This is where I am now:

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("stats.dat"));


// 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);


}
}
}

It takes a running count of all stats but I only want stats per player...how do I do that?

Here is the text file I'm reading from:

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

Edited by Nick Evan: Fixed formatting

0

You could use the firstIndexOf() method to return the occurence of the first ","...That method will return an int value, and once you have that, then you can create a substring with just the name, and that will also make it to were you can create a substring of everything to the right, and use the code I gave you in the first post.

String input = file.readLine();
int point = input.firstIndexOf(",");

String name = input.substring(0,point);
String records = input.substring(point,input.length());

That's something what it will look like.

0

It takes a running count of all stats but I only want stats per player...how do I do that?

Hmm...You would have to read one line at a time, and once finished with the calculations, you would have to reset the variables.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.