1

I need to remove any element of ArrayList, from user input and without using java iterator:

(see (switch) case 2:)

when I select Option 2 and proceed to input a name, for example James, it wont do anything as the list of friends would be the same. Any help would be much appreciated!

import java.util.Scanner;
import java.util.ArrayList;

public class FriendsTest
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);

      // objects
      ArrayList<Friends> friendsList = new ArrayList<>();

      Friends a1 = new Friends("James", 10);
      Friends a2 = new Friends("Christopher", 17);
      Friends a3 = new Friends("George", 25);
      Friends a4 = new Friends("Linda", 31);
      Friends a5 = new Friends("Karen", 62);

      friendsList.add(a1);
      friendsList.add(a2);
      friendsList.add(a3);
      friendsList.add(a4);
      friendsList.add(a5);

      // menu

      int menu_choice;

      String name;
      int age;

      do
      {
         System.out.println("\n1. Add a Friend");
         System.out.println("2. Remove a Friend");
         System.out.println("3. Display all Friends");
         System.out.println("4. Exit\n");

         System.out.print("\nSelect one option: ");
         menu_choice = input.nextInt();

         switch (menu_choice)
         {
            case 1:
               System.out.print("Enter Friend's name: ");
               name = input.next();

               System.out.print("Enter Friend's age: ");
               age = input.nextInt();

               Friends a6 = new Friends(name, age);
               friendsList.add(a6);
               break;

            case 2:
               System.out.print("Enter Friend's name to remove: ");
               name = input.next();

               friendsList.remove(name);
               break;

            case 3:
               for(int k = 0; k < friendsList.size(); k++)
               {
                  System.out.println(friendsList.get(k).name + " " + friendsList.get(k).age);
               }
               break;

            case 4:
               System.exit(0);

         }//end switch        



      } while (menu_choice != 4);


   }//end main



}//end class

Update with my constructor and methods class

public class Friends 
   {
      public String name;
      public int age;

      // parameters
      public Friends(String _name, int _age)
      {
         name = _name;
         age = _age;
      }

       // set name
       public void setName(String friendName)
       {
          name = friendName;
       }

       // get name
       public String getName()
       {
          return name;
       }

       // set age
       public void setAge(int friendAge)
       {
          age = friendAge;
       }

       // get age
       public int getAge()
       {
          return age;
       }

       // return toString()
       public String toString()
       {
       return getName() + " " + getAge();
       }

    } //end clas
6
  • The problem is that you're trying to remove a string from an ArrayList of objects that are very much not strings. You'll need to identify which item has the name entered. Why can't you use an iterator? Commented Nov 29, 2015 at 23:46
  • 2
    Your Friends will need to override (and correctly implement) the equals and hashcode methods. You should then be able to use ArrayList#remove(T) to remove and element by creating a new Friends object with the EXACT same properties as the one you have in you List Commented Nov 29, 2015 at 23:48
  • I might be off but how did case 2 even compile? You're trying to remove a String from an ArrayList of Friends. Commented Nov 29, 2015 at 23:55
  • Yeah, its a homework so we "haven't" yet seem iterators in java, is part of the specification of the homework... Commented Nov 29, 2015 at 23:57
  • @Jsch Have you seen a loop? Because essentially ANY answer will use a form of iteration within it's own implementation Commented Nov 30, 2015 at 0:01

2 Answers 2

3

A simple mechanism to use is removeIf. Something like:

friendsList.removeIf(friend -> friend.hasName(name);

Note this uses Java 8.

That is assuming a hasName method of Friend. If you only have getName then:

friendsList.removeIf(friend -> friend.getName().equals(name));
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I'm very new in java. But how do you apply ("whatevernamehere") dynamically so the user is the one who selects which friend to remove. I don't quite understand well, thanks!
I'll update my main post with my second class (constructor and methods)
1

Simple problem: instead of passing a Friends Object to the remove() method, you pass just the String. Write a custom remove code.

Also keep in mind to always do indexed deleting from bottom to top to not jump entries:

            System.out.print("Enter Friend's name to remove: ");
            name = input.next();

            //                  friendsList.remove(name);
            System.out.println("Trying to locate <" + name + ">");
            for (int i = friendsList.size() - 1; i >= 0; --i) {
                final Friends f = friendsList.get(i);
                System.out.println("\tChecking with <" + friendsList.get(i).name + ">");
                if (f.name.equals(name)) {
                    System.out.println("Found that bitch at index " + i);
                    friendsList.remove(i);
                    System.out.println("... and kicked him out");
                }
            }

            break;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.