This article has been dead for over three months
You
Okay, I'm doing a program where a menu pops up and it deals with the roster of a basketball team. You can add, edit, delete, and view the players. All of this is working but my problem is with sorting the players. I'm using an ArrayList (ArrayList contains each player's name, number, height, weight, etc.)object for the roster. I've been trying to use Collections.sort but it seems I'm doing something wrong. Anyone help would be greatly appreciated.
Here's the code if you need to see it:(Code referring to this problem is bold)
//Assign8.java
//A program that produces a menu and adds, changes, deletes, or shows the
//players on the Bulls Roster
//by James Borum
//October 12,2004
import java.util.*;
import java.io.*;
public class Assign8
{
//allows input from keyboard
private static BufferedReader input = new BufferedReader
(new InputStreamReader(System.in));
/*public static class StringComparator implements Comparator
{
// Compare two Strings. Callback for sort.
// effectively returns a-b;
// e.g. +1 (or any +ve number) if a > b
// 0 if a == b
// -1 (or any -ve number) if a < b
public final int compare ( Object a, Object b)
{
return((String )a).compareTo( (String)b);
} // end compare
} // end class StringComparator*/
public static class Player
{
//declcolumntions
private String number;
private String name;
private String position;
private String height;
private double weight;
private String birthDay;
private String experience;
private String college;
public Player()
{
//default constructor
}
public Player(String n,String na,String p, String h, double w,String bd, String e, String c)
{//multi-arguement constructor
setNumber(n);
setName(na);
setPosition(p);
setHeight(h);
setWeight(w);
setBirthDay(bd);
setExperience(e);
setCollege(c);
}
public void setNumber(String n)
{//method sets number variable
number = n;
}
public void setName(String na)
{//method sets name variable
name = na;
}
public void setPosition(String p)
{//method sets position variable
position=p;
}
public void setHeight(String h)
{//method sets height variable
height=h;
}
public void setWeight(double w)
{//method sets weight variable
weight=w;
}
public void setBirthDay(String bd)
{//method sets birthday variable
birthDay=bd;
}
public void setExperience(String e)
{//method sets experience variable
experience=e;
}
public void setCollege(String c)
{//method sets college variable
college = c;
}
public String getNumber()
{//returns number variable
return number;
}
public String getName()
{//returns name variable
return name;
}
public String getPosition()
{//returns position variable
return position;
}
public String getHeight()
{//returns height variable
return height;
}
public double getWeight()
{//returns weight variable
return weight;
}
public String getBirthDay()
{//returns birthday variable
return birthDay;
}
public String getExperience()
{//returns experience variable
return experience;
}
public String getCollege()
{//returns college variable
return college;
}
public void PrintInfo()
{//displays player info
System.out.print(new PrintfFormat("%-10s").sprintf(number));
System.out.print(new PrintfFormat("%-20s").sprintf(name));
System.out.print(new PrintfFormat("%-15s").sprintf(position));
System.out.print(new PrintfFormat("%-10s").sprintf(height));
System.out.print(new PrintfFormat("%-10g").sprintf(weight));
System.out.print(new PrintfFormat("%-14s").sprintf(birthDay));
//System.out.print(new PrintfFormat("%-10s").sprintf(getExperience()));
//System.out.print(new PrintfFormat("%-10s").sprintf(getCollege()));
System.out.print(new PrintfFormat("%-10s").sprintf(experience));
System.out.print(new PrintfFormat("%-10s").sprintf(college));
System.out.println();
}
}//end of Player class
public static void ProcessInfo(BufferedReader file, ArrayList team) throws IOException
{
String line = new String();
int i=0;
Player guy = new Player();
String column[] = new String[8];
column[0] = new String();
column[1] = new String();
column[2] = new String();
column[3] = new String();
column[4] = new String();
column[5] = new String();
column[6] = new String();
column[7] = new String();
StringTokenizer token;
line = file.readLine();
while(line != null)
{
guy = new Player();
token = new StringTokenizer(line, ",");
for (int j=0; j<8; ++j)
{
column[j]=token.nextToken();
}
guy.setNumber(column[0]);
guy.setName(column[1]);
guy.setPosition(column[2]);
guy.setHeight(column[3]);
guy.setWeight(Double.parseDouble(column[4]));
guy.setBirthDay(column[5]);
guy.setExperience(column[6]);
guy.setCollege(column[7]);
team.add(guy);
line = file.readLine();
++i;
}
}
public static void showMenu(ArrayList team) throws IOException
{
//displays menu options
System.out.println(" Chicago Bulls Roster ");
System.out.println("----------------------------------");
System.out.println("1 Show the list of players");
System.out.println("2 Add a player to roster");
System.out.println("3 Delete a player from roster");
System.out.println("4 Change a player on roster");
System.out.println("X Exit Program");
System.out.println("----------------------------------");
//declares variable that gets user selection
char answer='\0';
while (answer!='X')
{//while answer is not x
answer=input.readLine().charAt(0);
if (answer!='X')
switch(answer)
{//does switch statement using answer variable
case '1':
showPlayers(team);//method call to show roster
break;
case '2':
addPlayer(team);//method call to add player
break;
case '3':
deletePlayer(team);//method call to delete player
break;
case '4':
editPlayer(team);//method call to edit player
break;
default:
System.out.println("Invalid Entry!");
}
}//end of while
}//end of method
// method to display the entire roster
public static void showPlayers(ArrayList team) throws IOException
{
// create a temporary object of FootBallRec type
Player baller;
Player baller2;
baller = new Player();
baller2 = new Player();
System.out.print(new PrintfFormat("%-9s").sprintf("Number"));
System.out.print(new PrintfFormat("%-20s").sprintf("Name"));
System.out.print(new PrintfFormat("%-15s").sprintf("Position"));
System.out.print(new PrintfFormat("%-10s").sprintf("Height"));
System.out.print(new PrintfFormat("%-9s").sprintf("Weight"));
System.out.print(new PrintfFormat("%-10s").sprintf("Birthday"));
System.out.print(new PrintfFormat("%-12s").sprintf("Experience"));
System.out.print(new PrintfFormat("%-10s").sprintf("College"));
System.out.println();
System.out.print(new PrintfFormat("%-9s").sprintf("------"));
System.out.print(new PrintfFormat("%-20s").sprintf("---------"));
System.out.print(new PrintfFormat("%-15s").sprintf("----------"));
System.out.print(new PrintfFormat("%-10s").sprintf("------"));
System.out.print(new PrintfFormat("%-9s").sprintf("--------"));
System.out.print(new PrintfFormat("%-12s").sprintf("----------"));
System.out.print(new PrintfFormat("%-12s").sprintf("--------"));
System.out.print(new PrintfFormat("%-10s").sprintf("--------"));
System.out.println();
//Collections.sort(team);
//ArrayList list2 = new ArrayList();
//for (int j=0; j // method to add a player to the roster
public static void addPlayer(ArrayList team) throws IOException
{
String name2, position2,height2,bday, collegeAttended,jerseyNumber;
String experience2;
double weight2;
Player f;
System.out.println("Enter player number: ");
jerseyNumber=input.readLine();
System.out.println("Enter player name: ");
name2=input.readLine();
System.out.println("Enter player position: ");
position2=input.readLine();
System.out.println("Enter player height: ");
height2=input.readLine();
System.out.println("Enter player weight: ");
weight2=Double.parseDouble(input.readLine());
System.out.println("Enter player date of birth: ");
bday=input.readLine();
System.out.println("Enter player experience: ");
experience2=input.readLine();
System.out.println("Enter player college attended: ");
collegeAttended=input.readLine();
f=new Player(jerseyNumber, name2, position2, height2, weight2, bday, experience2, collegeAttended);
team.add(f);
showMenu(team);
}
public static void deleteFromList(ArrayList team, String lName, String pPos) throws IOException
{
// create a temporary object of FootBallRec type
Player f;
for (int j=0; j