Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Baseball stats error

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Baseball stats error

    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)));
    wCount=0;hCount=0;sCount=0;oCount=0;
    }
    }
    }
    the output shows the name and their stats correctly, but when counting outs or w/e it also counts os in the name so the numbers aren't correct is there any way to fix this?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Baseball stats error

    but when counting outs or w/e it also counts os in the name so the numbers aren't correct is there any way to fix this?
    Can your code detect what you don't want to count and not count it?

    Specifically what variables are being incremented when they should not be?
    What condition or data contents allows that to happen?

    You need to show the input to your program and the program's output and explain what is wrong with the output values.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Baseball stats error

    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

    thats the file it reads from but the output says that e.g. Willy Wonk has 12 outs when he only has 11

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Baseball stats error

    How does your program count the outs?
    What is different about the line for Willy Wonk and the other lines where the code works?
    Why does your program work for the other lines?

    What is your logic for counting the outs?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Baseball stats error

    You need to see how your code is executing by adding some printlns to see what values are being used. For example print out the value of the char returned by chatAt() to see what your code is looking at.

Similar Threads

  1. Reading CSV file to take min, max, average of various stats
    By Annorax in forum Collections and Generics
    Replies: 1
    Last Post: March 3rd, 2011, 08:57 PM
  2. Bissection Method Baseball Question
    By nickcanada in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 15th, 2010, 07:49 PM