3

Program is to take input from text file containing a player's number and stats. If a player is already in the list, I want to update that player's stats. I am having trouble with the method which determines if a player is already on the list. The current method prints all players regardless. Does the problem lie with my code in the main class or method?

public class Baseball8
{

    public static void main(String[] args) throws FileNotFoundException
    {

    Scanner fin = new Scanner(new FileReader("baseball.txt"));

    final int LIST_LENGTH = 20;

    int number = 0,         // number, hits, walks, outs
        hits = 0,
        walks = 0,
        outs = 0,
        players = 0,
        index = 0,
        teamSize = 0;
        playerIndex = 0;


    System.out.println("This program tracks a baseball player's number "
                         + "and their\number of walks, runs and outs for "
                         + "each game in a season.\n");


    Player team[] = new Player[LIST_LENGTH];

    int i;

    for(i = 0; i < LIST_LENGTH; i++)
        team[i] = new Player();



while (fin.hasNext())
    {
           number = fin.nextInt();
           hits = fin.nextInt();
           walks = fin.nextInt();
           outs = fin.nextInt();


        System.out.println(number + " " + hits + " " + walks + " " + outs + " ");



        playerIndex = findPlayerIndex(team,teamSize,number);


        if(playerIndex == -1)
        {
            team[teamSize].setNumber(number);
            team[teamSize].setHits(hits);
            team[teamSize].setWalks(walks);
            team[teamSize].setOuts(outs);
            teamSize++;
        }

        else
        {
            team[index].setHits(hits + team[index].getHits());
            team[index].setWalks(walks + team[index].getWalks());
            team[index].setOuts(outs + team[index].getOuts());
        }
   }
displayArray(team, teamSize);
fin.close();

}

.

public static int findPlayerIndex(Player p[], int n, int number)
{
    int i;

    for(i = 0; i < n; i++);
    {
        if(p[i].getNumber() == number)
            return i;
       else
           return -1;
    }
}


    **Current output:**
//player hits, walks, outs are added to array[0]
     Player Hits    Walks   Outs
------  ----    -----   ----

 1   5  10  18     <
19   0   5   1
 2   0   0   6
18   4   2   0
 4   1   2   3
12   2   2   2
 7   0   0   3
 8   1   4   1
10   2   2   2
 3   2   1   3
11   6   0   0
17   4   2   0
 9   3   2   1


Desired output:
//stats added to array[i]
Player  Hits    Walks   Outs
------  ----    -----   ----

 1   2   2   2
19   0   5   7 <
 2   0   5   7
18   4   2   0
 4   3   3   6 <
12   2   2   2
 7   0   0   6 <
 8   1   4   1
10   2   2   2
 3   3   3   6 <
11   6   0   0
17   4   2   0
 9   3   2   1

2 Answers 2

3

There are two problems in your function findPlayerIndex

  • remove ; after for so loop can work: for(i = 0; i < n; i++); //remove ;

  • return -1 after loop finished, int i can declare in loop command. Like this:

    for (int i = 0; i < n; i++) {
        if(p[i].getNumber() == number)
           return i;      
    }
    
    return -1;
    

Have fun!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, there are no longer duplicate players. Now I have to figure out how to add the stats properly.
2

You'll need to return -1 after the loop rather than within the loop. i.e.

for(i = 0; i < n; i++)
{
     if(p[i].getNumber() == number)
         return i;    
}
return -1;

This essentially ensures that you've checked the entire array and then if the predicate within the for loop is not met then -1 will be returned.

6 Comments

I understand why it should return -1 outside of the loop, otherwise it returns regardless. However, I still get the same output. A friend suggested updating the return type to void in the method, but that doesn't seem appropriate.
well, you mentioned I am having trouble with the method which determines if a player is already on the list. hence I've suggested an answer to it. Nevertheless please show the current result you're getting and the expected result, also show all the code necessary.
I was just trying to show that I follow your explanation. I am updating the original question to include current and desired output.
including the array declaration and variables initialized.
@bap you mentioned that The current method prints all players regardless. that's because of your displayArray prints all of them out. what data do you want that method to print?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.