ggatwork 0 Newbie Poster

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
        [B]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<team.size(); ++j)
        //  {
        //      baller.getNumber()
        //      list2.add(baller2);
        //  }
            //Collections.sort(list2);
            //System.out.print(list2);
            //Collections.sort(team, new StringComparator() );
            Collections.sort(team, new Comparator() {
            public int compare(Object o1, Object o2) {

                String s1 = (String) o1;
                String s2 = (String) o2;
                return s1.compareTo(s2);  

            }
        });
            for (int j=0; j<team.size(); ++j )  // (note the use of .size() instead of .length)
            {
                // get element from the list and assign to object
                // note the cast operation
                //System.out.print(list2);
                baller=(Player)team.get(j);  
                baller.PrintInfo();
            }

            System.out.println(team.size());
            System.out.println();
            showMenu(team);
        }//end of method[/B]    // 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<team.size(); ++j )  // (note the use of .size() instead of .length)
            {
                // get element from the list and assign to object
                // note the cast operation
                f=(Player)team.get(j);  

                if ((f.getName().equals(lName)) && (f.getPosition().equals(pPos)))
                  {
                    System.out.println("Found at record: " + j);
                    f.PrintInfo();
                    team.remove(j);
                  }
            }
            showMenu(team);
        }

        // method to remove a player from the roster
        public static void deletePlayer(ArrayList team) throws IOException
        {
            String lName;
            String pPos;

            System.out.println("Enter player name to delete: ");
            lName=input.readLine();
            System.out.println("Enter player position: ");
            pPos=input.readLine();

            deleteFromList(team, lName, pPos);
        }

        // method to change a player's information
        public static void editPlayer(ArrayList team) throws IOException
        {
            String bnumber;
            String bname;
            String bposition;
            String bheight;
            String bweight2;
            double bweight;
            String bbirthDay;
            String bexperience2;
            String bexperience;
            String bcollege;
            int j;
            Player f=null;
            boolean found=false;

            System.out.println("Enter player number: ");
            bnumber=input.readLine();

            for (j=0; j<team.size(); ++j )  // (note the use of .size() instead of .length)
            {
                f=(Player)team.get(j);  
                if (f.getNumber().equals(bnumber))
                  {
                    System.out.println("Found at record: " + j);
                    found=true;
                    f.PrintInfo();
                    break;                  
                  }
            }

            if (found)
            {
                System.out.print("Enter player's number: " + f.getNumber() + " (Enter for no change) ");
                bnumber=input.readLine();
                if (bnumber.equals("")) bnumber=f.getNumber();

                System.out.print("Enter player's name: " + f.getName() + " (Enter for no change) ");
                bname=input.readLine();
                if (bname.equals("")) bname=f.getName();

                System.out.print("Enter player's position: " + f.getPosition() + " (Enter for no change) ");
                bposition=input.readLine();
                if (bposition.equals("")) bposition=f.getPosition();

                System.out.print("Enter player's height: " + f.getHeight() + " (Enter for no change) ");
                bheight=input.readLine();
                if (bheight.equals("")) bheight=f.getHeight();

                System.out.print("Enter player weight: " + f.getWeight() + " (Enter for no change) ");
                bweight2=input.readLine();
                if (bweight2.equals("")) 
                  bweight=f.getWeight();
                else
                  bweight=Double.parseDouble(bweight2);
                    System.out.print("Enter player's date of birth: " + f.getBirthDay() + " (Enter for no change) ");
                bbirthDay=input.readLine();
                if (bbirthDay.equals("")) bbirthDay=f.getBirthDay();

                System.out.print("Enter player's experience: " + f.getExperience() + " (Enter for no change) ");
                bexperience=input.readLine();
                //bexperience2=Integer.parseInt(bexperience);
                if (bexperience.equals("")) bexperience=f.getExperience();

                System.out.print("Enter player's college attended: " + f.getCollege() + " (Enter for no change) ");
                bcollege=input.readLine();
                if (bcollege.equals("")) bcollege=f.getCollege();

                f=new Player(bnumber, bname, bposition, bheight, bweight, bbirthDay, bexperience, bcollege);
                team.set(j,f);
            }
            else
                System.out.println("Record Not Found!");


                showMenu(team);
        }
            // method to write the data in comma delimited format back to original file
        public static void writeFile(ArrayList team) throws IOException
        {
            FileWriter writer = new FileWriter("\\Bulls Roster.txt");
            PrintWriter outFile=new PrintWriter(writer);

            Player f;
            for (int j=0; j<team.size(); ++j )  
            {
                f=(Player)team.get(j);
                outFile.println(f.getNumber()+"," +f.getName()+"," +f.getPosition()
                +"," +f.getHeight()+"," +f.getWeight()+"," +f.getBirthDay()+"," +f.getExperience()+
                "," +f.getCollege());
            }
            writer.close();
        }   
    public static void main(String args[]) throws IOException
    {
        //FileReader object contains bulls text file
        FileReader read = new FileReader("\\bulls.txt");
        BufferedReader file = new BufferedReader(read);
        // create an ArrayList to hold the team array
        ArrayList team=new ArrayList();
        ProcessInfo(file, team);//method call
        read.close();  // close the master file
        showMenu(team);
        writeFile(team);
    }//end of main method
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.