Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're creating a new <code>ArrayList&lt;Friend&gt;</code> every time Add, Remove, or Display is called. You need to save your list as a class-level field.</p> <p>It would also be best if you took away all of the static methods and made them all instance level. Then your <code>FriendList</code> instance would be associated with a particular list of friends.</p> <p>For example:</p> <pre><code>public class FriendsList { private List&lt;Friend&gt; friends = new ArrayList&lt;Friend&gt;(); public void addFriends() { Scanner input = new Scanner( System.in ); System.out.println("\n--------------"); System.out.println("Please enter Name"); String name = input.nextLine(); //reads name System.out.println("Please enter Age"); int Age = input.nextInt(); friends.add(new Friends( Age, name)); }//end AddFriends public void removeFriend() { Scanner input = new Scanner( System.in ); System.out.println("\n--------------"); System.out.println("Please enter Name of person you wish you remove"); String name = input.nextLine(); friends.remove(name); }//End RemoveFriend public void displayArray() { System.out.print(friends); } </code></pre> <p>That would mean that you need to actually use your <code>friendList</code> instance variable in your main method rather than calling those methods in a static manner (i.e. use <code>friendsList.addFriends()</code> instead of <code>FriendList.addFriends()</code>)</p> <p>Also, notice that I made the first letter of my methods lowercase. That's pretty much a standard in Java.</p> <p><em><strong>EDIT</em></strong></p> <p>Your current code is trying to remove a <code>String</code> from your <code>List&lt;Friends&gt;</code>. It's not removing anything because there are no <code>String</code> elements in the <code>List</code>. You're going to have to iterate through the list and find the <code>Friend</code> whose name is <code>name</code></p> <p>So your remove method should look something like this:</p> <pre><code>public void removeFriend() { Scanner input = new Scanner( System.in ); System.out.println("\n--------------"); System.out.println("Please enter Name of person you wish you remove"); String name = input.nextLine(); Friends friendToRemove = null; for (Friends friend : friends) { if (name.equals(friend.getName()) { friendToRemove = friend; } } if (friendToRemove != null) { friends.remove(friendToRemove) } }//End RemoveFriend </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload