4 Replies - 893 Views - Last Post: 12 April 2012 - 05:48 PM Rate Topic: -----

#1 redman8442  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-October 11

array list element removal

Posted 12 April 2012 - 12:55 PM

Ok I know this is long but this is my whole program so you can see what I am doing. Ok in my else if statement for my "remove" player. When I add players to it, say 3 different players and I want to remove a certain player from my array list. It removes the first and last entry of players. I have been staring at my for loop for a couple of hours now and my eyes are starting to spin, and all the letters are running together. Can anyone see what I have done wrong and lead my in the right direction??? Thank you in advance for the help.


public class Project1Cadenhead {

     
    public static void main(String[] args) throws IOException {
        boolean loop = true;
        Scanner keyboard = new Scanner(System.in);
        ArrayList<Player> list = new ArrayList<Player>();
        Player newPlayer = null;
          while (loop == true) {
            System.out.println("What operation next? (ADD, REMOVE, SHOW, SAVE, QUIT)?");
            String entry = keyboard.next();
            if (entry.equalsIgnoreCase("add")) {
                newPlayer = new Player();
                System.out.println("Please enter player’s Last name:");
                String lastName = keyboard.next();
                newPlayer.setLastName(lastName);
                System.out.println("Please enter player’s First name:");
                String firstName = keyboard.next();
                newPlayer.setFirstName(firstName);
                System.out.println("Please enter number of points scored for " + firstName + " " + lastName + ":");
                Integer pointsScored = keyboard.nextInt();
                newPlayer.setPointsScored(pointsScored);
                System.out.println("Please enter number of assists for " + firstName + " " + lastName + ":");
                Integer assists = keyboard.nextInt();
                newPlayer.setAssists(assists);
                System.out.println("Please enter penalty kick percentage (example 0.25 for 25%) for " +firstName+ " "   + lastName + ":");
                double kickRate = keyboard.nextDouble();
                newPlayer.setPenaltyKickRate(kickRate);
                list.add(newPlayer);
                System.out.println("** PLAYER ADDED **\n");
                System.out.println(newPlayer.toString());
            } else if (entry.equalsIgnoreCase("remove")) {
                System.out.println("Please enter last name of player to remove:");
                String lastName = keyboard.next();
                System.out.println("Please enter first name of player to remove");
                String firstName = keyboard.next();
                for (int i = 0; i < list.size(); i++) {
                    list.remove(i);
                }
                System.out.println("**REMOVED " + firstName + " " + lastName + "**");

            } else if (entry.equalsIgnoreCase("show")) {
                System.out.println(" ** SHOW ALL PLAYERS **: ");
                if (list.isEmpty()) {
                    System.out.println("There are currently no players in the list. Please add players to the list.");

                }
                for (int i = 0; i < list.size(); i++) {
                    System.out.println(list.get(i).toString());
                    System.out.println("Averages\t" + "Points: " + averagePointsScored(list) + " Assists: " + averageAssists(list)
                            + " Penalty Kick %: " + averagePenaltyKicks(list));
                    System.out.println("Totals:" + list.size());
                }

            } else if (entry.equalsIgnoreCase("save")) {
                File file = new File("PlayerData.txt");
                PrintWriter output = new PrintWriter(file);
                for (int i = 0; i < list.size(); i++) {
                    output.println(list.get(i).toString());
                }
                output.println("Totals:" + list.size());
                output.println("Averages:\t" + "Points: " + averagePointsScored(list) + " Assists: " + averageAssists(list)
                        + " Penalty Kick %: " + averagePenaltyKicks(list));
                output.close();
                System.out.println("** " + list.size() + " RECORD SAVED TO Playerdata.txt **");





            } else if (entry.equalsIgnoreCase("quit")) {
                loop = false;
                System.out.println("** GOODBYE! **");
            } else {
                System.out.println("Operation not recognized. Please choose from the available list.\n");
                loop = true;
            }
        }

    }

    /** This method will get the points scored from the list and calculate the
     * average between them
     * Precondition: The points scored will be sent down.
     * Post Condition: The average will have been calculated for the points scored.
     * @param list
     * @return will return the average for the points scored
     */
    private static Integer averagePointsScored(ArrayList<Player> list) {
        int average = 0;
        int totalValue = 0;

        for (int i = 0; i < list.size(); i++) {
            totalValue = list.get(i).getPointsScored() + totalValue;
        }
        average = totalValue / list.size();
        return average;
    }//end of averaePointsScored method
    
    /** This method will get the assists from the list and calculate the
     * average between them
     * Precondition: The assists will be sent down.
     * Post Condition: The average will have been calculated for the assists.
     * @param list
     * @return will return the average for the assists.
     */
    private static Integer averageAssists(ArrayList<Player> list) {
        int average = 0;
        int totalValue = 0;

        for (int i = 0; i < list.size(); i++) {
            totalValue = list.get(i).getAssists() + totalValue;
        }
        average = totalValue / list.size();
        return average;
    }//end of averageAssists method

    /** This method will get the penalty kick % from the list and calculate the
     * average between them.
     * Precondition: The Penalty kick % will be sent down.
     * Post Condition: The average will have been calculated for the penalty kick %.
     * @param list
     * @return will return the average for the penalty kick %.
     */
    private static double averagePenaltyKicks(ArrayList<Player> list) {
        double average = 0;
        double totalValue = 0;

        for (int i = 0; i < list.size(); i++) {
            totalValue = list.get(i).getPenaltyKickRate() + totalValue;
        }
        average = totalValue / list.size();
        return average;
    }//end of averagePenaltyKicks method
}


This post has been edited by redman8442: 12 April 2012 - 12:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: array list element removal

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: array list element removal

Posted 12 April 2012 - 01:08 PM

Funny one :)

First you should check if the name match, you don't. You simply remove all entries and not correctly:

for(int i = 0; i < list.size(); ++i)
    list.remove(i);


What happens ?
You enter the loop i == 0
you remove entry at index 0
entry at 1 replaces the one that was at 0
entry at 2 replaces the one that was at 1
...
entry at size() - 1 replaces the one that was at size() - 2
then you re-enter the loop i == 1
you remove entry at 1 the entry at 0 will stay there

three ways to do that

// less efficient
for(int i = 0; i < list.size(); ++i)
   list.remove(0);

// better, removing from the end nothing to shift
for(int i = list.size(); --i >= 0;)
    list.remove(i);

// the library one
list.clear();



But what you realy want is

for(int i = list.size(); --i >= 0;) {
   Player p = list.get(i);
   if(p.name.equals(name)) {
      list.remove(i);
   }
}


This post has been edited by pbl: 12 April 2012 - 01:11 PM
Reason for edit:: Add by name

Was This Post Helpful? 0
  • +
  • -

#3 redman8442  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-October 11

Re: array list element removal

Posted 12 April 2012 - 02:10 PM

Ok i wrote what you had there with my parameters and it deleted everything in my list. It really didn't step through the elements to find the name and remove that name element. Wow this shouldn't be that hard..... :(
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: array list element removal

Posted 12 April 2012 - 02:20 PM

            } else if (entry.equalsIgnoreCase("remove")) {
                System.out.println("Please enter last name of player to remove:");
                String lastName = keyboard.next();
                System.out.println("Please enter first name of player to remove");
                String firstName = keyboard.next();
                for (int i = list.size(); --i >= 0;) {
                    Player p = list.get(i);
                    if(!p.getLastName().equals(lastName))
                        continue;
                    if(!p.getFirstName().equals(firstName))
                        continue;
                    list.remove(i);
                    System.out.println("**REMOVED " + firstName + " " + lastName + "**");
                }


Was This Post Helpful? 0
  • +
  • -

#5 redman8442  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-October 11

Re: array list element removal

Posted 12 April 2012 - 05:48 PM

thank you for all of your help. :) I didn't have the not equal to. Again thank you!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1