My assignment:
develop a limited application to track the statistics of sports players. For each player, track first and last name, points scored, assists made, and penalty rate. Design and implement an object capable of recording this info. Implement this object as Player. Must allow user to enter new player info for any number of players, store all the info for each player, and allow certain queries to be made on player stats. Application must prompt user to perform operations (Add, Remove, Show, Save, Quit).

I have the most of the program completed. I have an issue with showing the players which basically prints the entire arrayList toString in a formatted manner. It looks like it should work but then again, it's a bit simplistic so why would it?

The other problem I am having is removing a player from the list. I know that arrayList.remove() should be a part of the solution but I don't know how to to find the player by the first and last name stored in each object.

If you need me to post my code for my player object, let me know.

Also if any of this looks like it sucks for whatever reason, please let me know. Although, improvements come after functionality.

This is homework. Please do not write my code for me. Any help otherwise would be greatly appreciated. If I find the answer before somebody posts I will update with my progress.

Here is my code:

package project1_playerstats;

/**
 * @author 24x24
 */
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Player> playerList = new ArrayList<Player>();
        int records = 0;
        userPrompt(playerList, records); // User prompt - Asks for following commands:
        // add player, remove player, show players, save, quit

    } // end of main method

    public static void userPrompt(ArrayList playerList, int records) throws FileNotFoundException {
        System.out.println("What operation next? (ADD, REMOVE, SHOW, SAVE, QUIT)? ");
        Scanner keyboard = new Scanner(System.in);
        String choice = keyboard.next().toUpperCase();
// needs user error check of if != choices print out not recognized, try again
        if (choice.equals("ADD")) {
            add(playerList, records); // calls add method
        } else if (choice.equals("REMOVE")) {
            remove(playerList, records); // calls remove method
        } else if (choice.equals("SHOW")) {
            show(playerList); // calls show method
        } else if (choice.equals("SAVE")) {
            save(playerList, records); // calls save method
        } else if (choice.equals("QUIT")) {
            quit(); // calls quit method
        }

    } // end userPrompt method

    public static void add(ArrayList playerList, int records) throws FileNotFoundException {

        System.out.println("");
        System.out.println("Please enter player's Last Name: ");
        Scanner keyboard = new Scanner(System.in);
        String lastName = keyboard.next().toUpperCase();
        
        System.out.println("");
        System.out.println("Please enter player's First Name: ");
        String firstName = keyboard.next().toUpperCase();
       
        System.out.println("");
        System.out.prinsome small issuestln("Please enter player's points scored: ");
        String pointsScored = keyboard.next().toUpperCase();
        
        System.out.println("");
        System.out.println("Please enter player's assists: ");
        String assists = keyboard.next().toUpperCase();
        
        System.out.println("");
        System.out.println("Please enter " + firstName + " " + lastName + " penalty kick precentage (example 0.25 for 25%) : ");
        String penaltyKickRate = keyboard.next().toUpperCase();
        
        Player player = new Player(lastName, firstName, pointsScored, assists, penaltyKickRate);
        // adds a new player object to playerList

        playerList.add(player);

        System.out.println("** PLAYER ADDED **");
        // need to make toString work. currently does nothing
        records++; // increments a count of the records
        player.toString();
        userPrompt(playerList, records);
    } // end of add method

    public static void remove(ArrayList playerList, int records) {

        System.out.println("Please enter the last name of player to be removed: ");
        System.out.println("");
        Scanner keyboard = new Scanner(System.in);
        String lastName = keyboard.next().toUpperCase();
        
        System.out.println("");
        System.out.println("Please enter the first name of player to be removed: ");
        String firstName = keyboard.next().toUpperCase();
        
        playerList.contains(lastName, firstName);
        playerList.remove(index);
        System.out.println("** REMOVED " + firstName + " " + lastName + " **");

// THIS DOESN'T WORK
        // removes a player from the list
        // decrements records
        records--; // decrements count of records
    } // end of remove method

    public static void show(ArrayList playerList) {

        playerList.toString();

        // shows the current player list
    } // end of show method

    public static void save(ArrayList playerList, int records) throws FileNotFoundException {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter file name: ");
        String fileName = keyboard.next().toUpperCase();
       
        PrintWriter out = new PrintWriter(fileName);
        out.print(playerList);
        out.close();

        System.out.println("** " + records + " " + " RECORD SAVED TO " + fileName);
        userPrompt(playerList, records);
        // save the current player list
        
    } // end of save method

    public static void quit(){
        
            System.out.println("** GOODBYE! **");
            System.exit(0);  // ends the program
        }

    } // end of quit method
} // end of Main class

Thanks again for any help!

OK so I sort of have a better show method:

System.out.println(playerList.toString());

I for some reason thought I could just type toString and it would magically print it out. This now prints:
[
null null: Points: 0 Assists: 0 Penalty Kick Rate: 0.0]

Which means it is pulling the basic values but I did this after I added a player so I still need to work on that.

Could you please post the Player class and show how you override the toString() method? :)


In line 55. there's something seriously wrong...

System.out.prinsome small issuestln("Please enter player's points scored: ");

To remove a player, you could look in each player in the player ArrayList, and check which one has the same first and last name, do you have an idea of how could you do that?

Wow, that must have come from me accidentally brushing the touchpad on my laptop while I was typing. If you remove "some small issues" from it you get println. I'll edit that in my post. My player class is as follows:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project1_playerstats;

/**
 * @author 24x24
 */
public class Player {

    private String lastName;
    private String firstName;
    private int pointsScored;
    private int assists;
    private double penaltyKickRate;

    // Basic constructor
    /**
     *
     * @param lastName
     * @param firstName
     * @param pointsScored
     * @param assists
     */
    public Player(String lastName, String firstName) {
    }

    // Complete constructor
    /**
     *
     * @param lastName
     * @param firstName
     * @param pointsScored
     * @param assists
     * @param penaltyKickRate
     */
    

    public Player(String lastName, String firstName, String pointsScored, String assists, String penaltyKickRate) {
 
    }

    // ------------------
    // Methods
    // ------------------
    //
    // Get the value of lastName
    // @return the value of lastName
    public String getLastName() {
        return lastName;
    }
    // Set the value of lastName

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    // Get the value of firstName
    // @return the value of firstName
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    // Get the value of pointsScored

    // @return the value of pointsScored
    public double getPointsScored() {
        return pointsScored;
    }

    public void setPointsScored(int pointsScored) {
        this.pointsScored = pointsScored;
    }

    public int getAssists() {
        return assists;
    }

    public void setAssists(int assists) {
        this.assists = assists;
    }
    // Get the value of penaltyKickRate
    // @return the value of penaltyKickRate

    public double getPenaltyKickRate() {
        return penaltyKickRate;
    }

    public void setPenaltyKickRate(double penaltyKickRate) {
        this.penaltyKickRate = penaltyKickRate;
    }

    // Netbeans generated
    @Override
    public String toString() {
        return "\n"+ firstName + " " + lastName + ": " + "\tPoints: " + pointsScored + "\tAssists: " + assists + "\tPenalty Kick Rate: " + penaltyKickRate;
    }
} // End of Player class

Thanks for taking a look!

I'm not positive why but for some reason it won't let me edit my previous post. Everyone please realize that I'm on a 12" laptop and can't type and that is why there is an error in line 55 :)

In your Player complete constructor you don't seem to assign the values of the variables you got to the ones in the class, that's why when you print your Players the data is all strange, because they weren't assigned. And when you get data from the user (through the Scanner), it doesn't need to be all Strings, check out the nextInt() And nextDouble() methods in the Scanner class, if you get all in Strings you'd have to parse the values in the class and that would be just waste of effort.
Also, the show method just gets the String value of the ArrayList of players, and doesn't print it or do anything with it, apart from that, since you can save the data of the players, wouldn't you want to see the data you saved when you choose SHOW?
Yet, I think the PrintWriter will just erase everything that's been written before in the file (please ignore the rest if I'm wrong), so you should just append the new players to the end of the file.


Ps. You may wanna change the printing of the array, because it will just be [info about player1, info about player 2,...], and that's kind of strange, but it's your choice.

Ok so I figured out what I was doing wrong. The add feature was actually not functioning properly due to me not putting the attribute variables into the player once I asked the user for them. Once I did that the show method worked. The remove method was just in need of a loop that would cycle through all of the players and find a name. Here is the finished product minus the player class (Please do not copy my code if you are doing homework. I am posting this for learning purposes only. If you need help, please post questions to the forum):

/*
 * This program will asks the player what they want to do with a user prompt
 * and then it calls the method that corresponds to the selected command. The
 * commands will either add a player to the list, remove a player from the list,
 * show the list in it's entirety, save the list to a file, or quit the program.
 * This program uses a Player class that helps to define the player object that
 * is in use.
 */
package project1_playerstats;

/**
 * @author 24x24
 */
import java.io.*;
import java.util.*;

public class Main {
    // Creating the player list
    private static ArrayList<Player> playerList;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
        playerList = new ArrayList<Player>(); // create the arraylist
        userPrompt(); // User prompt - Asks for following commands:
            // add player, remove player, show players, save, quit

    } // end of main method

    /**
     * This method is the User Prompt. It asks the player what they want to do
     * and then it calls the method that corresponds to the command. If the
     * command is not valid the prompt will ask the user to try again.
     * Pre Condition User must select a command
     * Post Condition User Prompt will call a method
     * @throws FileNotFoundException
     */
    public static void userPrompt() throws FileNotFoundException {
        System.out.print("What operation next? (ADD, REMOVE, SHOW, SAVE, QUIT)?: ");
        Scanner keyboard = new Scanner(System.in);
        String choice = keyboard.next().toUpperCase();

        if (choice.equals("ADD")) {
            add(); // calls add method
        } else if (choice.equals("REMOVE")) {
            remove(); // calls remove method
        } else if (choice.equals("SHOW")) {
            show(); // calls show method
        } else if (choice.equals("SAVE")) {
            save(); // calls save method
        } else if (choice.equals("QUIT")) {
            quit(); // calls quit method
        } else if (!choice.equals("ADD") || !choice.equals("REMOVE") || !choice.equals("SHOW") || !choice.equals("SAVE") || !choice.equals("QUIT")) {
            System.out.println("Operation not recognized. Please try again");
            userPrompt();
        }

    } // end userPrompt method

    /**
     * This method adds a new playe to the player list. It asks the user for
     * Last name, First name, Points scored, Assists and PKR. It then returns to
     * the User Prompt
     * Pre Condition User must have selected add
     * Post Condition program will add a player to player list
     * @throws FileNotFoundException
     */
    public static void add() throws FileNotFoundException {

        System.out.println("");
        System.out.println("Please enter player's Last Name: ");
        Scanner keyboard = new Scanner(System.in);
        String lName = keyboard.next().toUpperCase();
        // User error if no last name is entered
        if ((lName == null) || (lName.equals(" "))) {
            throw new IllegalArgumentException();
        }
        System.out.println("");
        System.out.println("Please enter player's First Name: ");
        String fName = keyboard.next().toUpperCase();
        // User error if no first name is entered
        if ((fName == null) || (fName.equals(" "))) {
            throw new IllegalArgumentException();
        }
        System.out.println("");
        System.out.println("Please enter points scored for " + fName + " " + lName + ": ");
        int points = keyboard.nextInt();
        // User error if no score is entered
        if ((points == ' ') || (points == (0))) {
            throw new IllegalArgumentException();
        }
        System.out.println("");
        System.out.println("Please enter assists for " + fName + " " + lName + ": ");
        int assisted = keyboard.nextInt();
        // User error if no assists are entered
        if ((assisted == ' ') || (assisted == (0))) {
            throw new IllegalArgumentException();
        }
        System.out.println("");
        System.out.println("Please enter penalty kick precentage (example 0.25 for 25%) for " + fName + " " + lName + ": ");
        double penalty = keyboard.nextDouble();
        // User error if no penalty is entered
        if ((penalty == 0) || (penalty == (' '))) {
            throw new IllegalArgumentException();
        }
        Player player = new Player(lName, fName, points, assisted, penalty);
        // adds a new player object to playerList
        playerList.add(player);
        // prints confirmation of new player being added
        System.out.println("** PLAYER ADDED **");
        System.out.println(player);

        userPrompt();
    } // end of add method

    /**
     * This method removes the player that the user enters and then returns to
     * the User Prompt. If it doesn't find the player it tells the user and then
     * returns to the user prompt.
     * Pre Condition User must have selected remove
     * Post Condition program will remove selected player
     * @throws FileNotFoundException
     */
    public static void remove() throws FileNotFoundException {
        System.out.println("");
        System.out.println("Please enter the last name of player to be removed: ");
        Scanner keyboard = new Scanner(System.in);
        String lastName = keyboard.next();
        // User error if no last name is entered
        if ((lastName == null) || (lastName.equals(" "))) {
            throw new IllegalArgumentException();
        }
        System.out.println("");
        System.out.println("Please enter the first name of player to be removed: ");
        String firstName = keyboard.next();
        // User error if no first name is entered
        if ((firstName == null) || (firstName.equals(" "))) {
            throw new IllegalArgumentException();
        }

        // loop through the player list to find and remove the name entered
        for (int i = 0; i <= playerList.size() - 1; ++i) {
            if (playerList.get(i).getFirstName().equalsIgnoreCase(firstName)
                    && playerList.get(i).getLastName().equalsIgnoreCase(lastName)) {
                playerList.remove(i);

                System.out.println("** REMOVED " + firstName + " " + lastName + " **");

                userPrompt(); // breaks out of loop when it removes the name
            }
        }
        // If name is not found in player list
        System.out.println("** WARNING " + firstName + " " + lastName + " NOT FOUND! **");

        userPrompt();
    } // end of remove method

    public static void show() throws FileNotFoundException {

        System.out.println(playerList);
        System.out.println("Total records: " + playerList.size());
        // shows the current player list
        userPrompt();
    } // end of show method

    /**
     * This method saves the added records. It then returns to the User Prompt
     * Pre Condition User must have selected save
     * Post Condition A file will be saved to the project folder
     * @throws FileNotFoundException
     */
    public static void save() throws FileNotFoundException {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter file name: ");
        String fileName = keyboard.next();
        // User error if no file name is entered
        if ((fileName == null) || (fileName.equals(" "))) {
            throw new IllegalArgumentException();
        }

        PrintWriter out = new PrintWriter(fileName);
        out.print(playerList);
        out.close();

        System.out.println("** " + playerList.size() + " " + " RECORD SAVED TO " + fileName);
        userPrompt();
        
    } // end of save method

    /**
     * This method quits the program. If it finds that there are players in the
     * player list it asks if the user wants to save before quitting
     * Pre Condition User must have selected quit
     * Post Condition program will end
     * @throws FileNotFoundException
     */
    public static void quit() throws FileNotFoundException {

        if (playerList.isEmpty()) { // if no records exist
            System.out.println("");
            System.out.println("** GOODBYE! **");
            System.exit(0);  // ends the program without saving
        } else {
            System.out.println("Do you want to save before you exit? (YES or NO) ");


        Scanner keyboard = new Scanner(System.in);
        String choice = keyboard.next().toUpperCase();
        // User error if no choice is entered
        if ((choice == null) || (choice.equals(" "))) {
            throw new IllegalArgumentException();
        } else if (choice.equals("YES")) {
            save(); // calls save method
        } else if (choice.equals("NO")) {
            System.out.println("");
            System.out.println("** GOODBYE! **");
            System.exit(0);  // ends the program
        } // end of else if No
        } // end of else loop if there is a record

    } // end of quit method
} // end of Main class

I put in a ton of error checking and even added a prompt asking the user if they wanted to save if it detected a record in the array.
Thanks for your help Yancouto!

Oh I also had an issue from the beginning where it would drop the entered text to the line after the next choice. I'm not sure why. I had my coworker look at that and he was confused as to why it was happening so I will do some work to try and figure that one out. It was due last night but I like to figure out what I don't understand.

This question has already been answered. Start a new discussion instead.