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