Updating the array list

The program should take input from a text file containing the player's number and statistics. If the player is already on the list, I want to update the player's stats. I am having problems with a method that determines if a player is on the list. The current method outputs all players regardless of them. Is the problem 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 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

      

+3


source to share


2 answers


There are two problems in your function: findPlayerIndex

  • delete; after that the loop can work: for (i = 0; i <n; i ++); // delete ;

  • return -1 after the end of the loop, int i can be declared in the loop command. Like this:

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



Good luck!

+3


source


You need to return -1

after the loop, not within the loop. i.e.

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

      



This will ensure that you have checked the entire array and then if the predicate in the loop for

fails, it returns -1

.

+2


source







All Articles