I'm working on a program that pulls data from an input file into an array. I am at the point where the data is executed properly, but I am having some syntax errors when it comes to sorting the array in a particular order. The text file that is being used as the input file has the following data:
1 2 2 2
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
2 0 5 1
19 0 0 6
17 4 2 0
9 3 2 1
4 2 1 3
3 1 2 3
7 0 0 3
The numbers above represent data of a baseball team where the first column is a players number, second column is hits for that player, third column is walks for that player, and the fourth column is outs for that player.
There is a class that is stored within the array that is below:
public class Player {
private int Number;
private int Hits;
private int Walks;
private int Outs;
Player() {
Number = Hits = Walks = Outs = 0;
}
public int getNumber() {
return Number;
}
public int getHits() {
return Hits;
}
public int getWalks() {
return Walks;
}
public int getOuts() {
return Outs;
}
public void setNumber(int n) {
Number = n;
}
public void setHits(int h) {
Hits = h;
}
public void setWalks(int w) {
Walks = w;
}
public void setOuts(int o) {
Outs = o;
}
// overload for output
public String toString() {
String s = new String();
s = String.format("%2d",Number) + "\t" +
String.format("%2d",Hits) + "\t" +
String.format("%2d",Walks) + "\t" +
String.format("%2d",Outs);
return s;
}
} // end of the class
Last, this is the code of my program. I'm getting syntax errors when it comes to sorting the array by player number. Player number is the only column that has to be in ascending order. Any advice would be appreciated.
import java.io.*;
import java.util.*;
// declaration of the class
public class Baseball9 {
// 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[LIST_LENGTH];
// 3a) loop over the teamsize
for (int i = 0; i < LIST_LENGTH; i++) {
// 3b) instantiate the i'th team member
team[i] = 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
int pos = findNumber(team, LIST_LENGTH, number);
// 7) if player number is not in the list
if (pos < 0) {
// 7a) set the Number field for team[teamSize]
team[teamSize].setNumber(number);
// 7b) set the Hits field for team[teamSize]
team[teamSize].setHits(hits);
// 7c) set the Walks field for team[teamSize]
team[teamSize].setWalks(walks);
// 7d) set the Outs filed for team[teamSize]
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[teamSize].setHits(hits + team[teamSize].getHits());
// 8b) update the Walks field for team[index]
team[teamSize].setWalks(walks + team[teamSize].getWalks());
// 8c) update the Outs field for team[index]
team[teamSize].setOuts(outs + team[teamSize].getOuts());
}
} // end while
// 9) display the results
displayArray(team, teamSize);
selectionSort(team, teamSize);
displayArray(team, teamSize);
// 10) disconnect from input file
fin.close();
} // end of main
// *************************************************************************
// determine the position within an array of Player of a player's index number
// performs a sequential search from the begining of the array
// returns -1 on a failure to find an index within the array
//**************************************************************************
// 1) assume that this player_ number is not in the list
public static int findNumber(Player list[], int listLength, int searchItem) {
int loc;
boolean found = false;
loc = 0;
// 2) loop over the list length
while (loc < listLength && !found)
// 3) exit the loop if the number is found
if (list[loc].getNumber() == searchItem)
found = true;
// 4) update the index on successful search
else
loc++;
// 5) return either the found index or -1
if (found)
return loc;
else
return -1;
}
//************************************************************************
// display players in an array of Player with formatting of
// Player #, Number of Hits, Number of Walks, Number of Outs
//************************************************************************
public static void displayArray(Player list[], 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(list[i]);
}
}
public static void selectionSort(Player list[], int listLength) {
int index;
int smallestIndex;
int minIndex;
int temp;
for (index = 0; index < listLength - 1; index++) {
smallestIndex = index;
for (minIndex = index + 1; minIndex < listLength; minIndex++)
if (list[minIndex] < list[smallestIndex])
smallestIndex = minIndex;
temp = list[smallestIndex];
list[smallestIndex] = list[index];
list[index] = temp;
}
}
} // end of the class