I have written what I can and have come to a point where I cant figure out why it is not saving the data. Below is the File Test, then the File, and the Array file I have made. please some help...
$
import java.util.Scanner;
public class FriendsListTest
{
public static void main( String[] args)
{
FriendsList friendsList = new FriendsList();
char selection;
Scanner input = new Scanner( System.in );
do
{
//display menu
System.out.println("\n--------------");
System.out.println("Main Menu\n");
System.out.println("1. Add a friend ");
System.out.println("2. Delete a friend ");
System.out.println("3. Show List ");
System.out.println("4. Exit");
//selection results
selection = input.next().charAt(0);
switch(selection){
case '1':
FriendsList.AddFriends();
break;
case '2':
FriendsList.RemoveFriend();
break;
case '3':
FriendsList.DisplayArray();
break;
case '4':
break;
default:
System.out.println("Invalid Selection");
}//end Switch
}while( selection != 4);//end do
}//end main
}//end FriendsListTest
$ $
import java.util.Scanner;
import java.util.ArrayList;
public class FriendsList
{
public static void AddFriends()
{
Scanner input = new Scanner( System.in );
ArrayList<Friends> friend = new ArrayList<Friends>(20);
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();
friend.add(new Friends( Age, name));
}//end AddFriends
public static void RemoveFriend()
{
ArrayList<Friends> friend = new ArrayList<Friends>(20);
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();
friend.remove(name);
}//End RemoveFriend
public static void DisplayArray()
{
ArrayList<Friends> friend = new ArrayList<Friends>(20);
System.out.print(friend);
}//end DisplayArray
}//end class FriendsList
$ $
class Friends
{
private String nName;
private int nAge;
public Friends(int Age, String name)
{
nName = name;
nAge = Age;
}
public String getName()
{
return nName;
}
public int getAge()
{
return nAge;
}
}//end class person
Ok after your suggestions I was able to get it to store data but now I have the problem of removing data. It doesn't throw any errors when removing data but the data remains. If I would repost the program if you with the edits that I have done.
You're creating a new ArrayList whenever you want to add or remove a Friend rather than acting on an ArrayList of the class. Do the latter please not the former. In other words give your class an ArrayList<Friend>
variable and do your friend additions and removals on that array list.
you need to understand what's the life scope of a local variable, apparently you need a member variable here. FYI: What is the difference between a member variable and a local variable?
You're creating a new ArrayList<Friend>
every time Add, Remove, or Display is called. You need to save your list as a class-level field.
It would also be best if you took away all of the static methods and made them all instance level. Then your FriendList
instance would be associated with a particular list of friends.
For example:
public class FriendsList
{
private List<Friend> friends = new ArrayList<Friend>();
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);
}
That would mean that you need to actually use your friendList
instance variable in your main method rather than calling those methods in a static manner (ie use friendsList.addFriends()
instead of FriendList.addFriends()
)
Also, notice that I made the first letter of my methods lowercase. That's pretty much a standard in Java.
EDIT
Your current code is trying to remove a String
from your List<Friends>
. It's not removing anything because there are no String
elements in the List
. You're going to have to iterate through the list and find the Friend
whose name is name
So your remove method should look something like this:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.