Pouyaan Pf Pouyaan Pf - 19 days ago 7
Java Question

What is wrong with method findNumber which performs sequential search in an array to find an index

I am supposed to get an output which shows player's numbers with their walks, outs, and hits, which comes from a .txt file.
I did most of the programming but th output shows -1 for all the players.
Where is the problem ?
Is it the findNumber method?
Or other syntax errors?

public class Baseball8
{

// implementation of main program
public static void main(String[] args) throws FileNotFoundException
{

// 1) connect to input file
Scanner fin = new Scanner(new FileReader("baseball.txt"));

// objects used to store data
final int LIST_LENGTH = 20;

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


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


// 3) declare an array of LIST_LENGTH players
Player[] team = new Player[20];
// 3a) loop over the teamsize
for ( index=0; index<LIST_LENGTH; index++)

// 3b) instantiate the i'th team member
{
team[index] = new Player();
}

// 4) loop on end of file
while (fin.hasNext())
{
// 5) attempt to input the data for the next Player
number = fin.nextInt();
hits = fin.nextInt();
walks = fin.nextInt();
outs = fin.nextInt();
// 6) find the index of this Player's numbEr

number = findNumber(team, index, teamSize);
// 7) if player number is not in the list
if(number == -1 )
{ // 7a) set the Number field for team[teamSize]
team[teamSize].getNumber();
team[teamSize].setNumber(number);
// 7b) set the Hits field for team[teamSize]
team[teamSize].getHits();
team[teamSize].setHits(hits);
// 7c) set the Walks field for team[teamSize]
team[teamSize].getWalks();
team[teamSize].setWalks(walks);
// 7d) set the Outs filed for team[teamSize]
team[teamSize].getOuts();
team[teamSize].setOuts(outs);
// 7e) increase teamSize by 1
teamSize++;
} // 8) else player number is in the list
else
{
// 8a) update the Hits field for team[index]
team[index].setHits(hits + team[index].getHits());
// 8b) update the Walks field for team[index]
team[index].setWalks(walks + team[index].getWalks());
// 8c) update the Outs field for team[index]
team[index].setOuts(outs + team[index].getOuts());
}
}


// 9) display the results
displayArray(team, teamSize);
// 10) disconnect from input file
fin.close();

} // end of main


public static int findNumber(Player[] team, int index,int teamSize )
{ // 1) assume that this player_ number is not in the list
int Save = -1;
// 2) loop over the list length
for (int i=0; i< teamSize - 1; i++)
{
if (index == team[i].getNumber())
Save = i;

}
return Save;
// 3) exit the loop if the number is found
// 4) update the index on successful search
// 5) return either the found index or -1
}

public static void displayArray(Player [] team, int team_size)
{
// 1) display headins of colums
System.out.println("\n\nPlayer\tHits\tWalks\tOuts\n"
+ "------\t----\t-----\t----\n");
// 2) loop over team size
for (int i=0; i < team_size; i++)
{
// 3) display i'th player
System.out.println(team[i]);
}
}


} // end of the class

Answer

By

output shows -1 for all the players.

I suppose you mean the number of all players has been set to -1? The findNumber-function gives the index of the player with the number you've given as an argument right?? So if you would make the following call:

findNumber(team, 10, 20);

it means you are searching for the player with number 10 in the team-array and you specify that the team-array represents a team with 20 players. The result of this function will then be the index(place) of the player in the team-array.

Well the reason your code is failing is this fragment of code:

    number = fin.nextInt();
    hits = fin.nextInt();
    walks = fin.nextInt();
    outs = fin.nextInt();
    // 6) find the index of this Player's numbEr

    number =    findNumber(team, index, teamSize);
    // 7) if player number is not in the list
    if(number == -1 )
    ...

You read the number of the player, his hits, walks and outs from the file (so far so good) but then you are searching for the player with the value of index as number and you are storing the index of that player (the result of the findNumber-function in your number-variable. So you replace the number you just read with the result of the findNumber function (that will always return -1 because you're looking for players with as number the value of the index that probably holds a number that doesn't exist in the team and never will because of this error). I don't think that's what you want...

What you probably want is to search the index of the player that has the number you just read from the file (stored in the number variable), store that index in the index variable and then check if that index, now in the index variable, is -1 (meaning, player with number number doesn't exist yet) or not. So the fragment above should be:

    number = fin.nextInt();
    hits = fin.nextInt();
    walks = fin.nextInt();
    outs = fin.nextInt();
    // 6) find the index of this Player's numbEr

    index = findNumber(team, number, teamSize);
    // 7) if player number is not in the list
    if(index == -1 )
    ...

I hope this helps!

In the code following this fragment you used your variables correctly.

Good luck!