简体   繁体   中英

Using an array to store input from the user

I'm currently working on this project:

  1. Create a simple Friends class with, as a minimum, the following:

    -name and age fields

    -appropriate constructors

    -get/set methods

    -toString() method

  2. Create an ArrayList of Friends.

  3. Write a program to manage your list of friends.

  4. Run the program from a menu with the following options:

    -Add a Friend

    -Remove a Friend

    -Display all Friends

    -Exit

and I've come a ways, but I'm not sure I'm heading in the right direction.

Here's what I've got so far:

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

public class Friends
{
    public static void main( String[] args )
    {
        int menu;
        menu = 0;
        int choice;
        choice = 0;


        Scanner input = new Scanner(System.in);
        ArrayList< Friends > name = new ArrayList<  >();
        ArrayList< Friends > age = new ArrayList<  >();

        System.out.println(" 1. Add a Friend ");
        System.out.println(" 2. Remove a Friend ");
        System.out.println(" 3. Display All Friends ");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

        while(menu != 4)
        {    

        switch(menu)
        {                     

        case 1:
            while(choice != 2)
            {
                System.out.println("Enter Friend's Name: ");
                name.add = input.next();
                System.out.println("Enter Friend's Age: ");
                age.add(input.nextInt());                    
                System.out.println("Enter another? 1: Yes, 2: No");
                choice = input.nextInt();
            } break;

            case 2:
                System.out.println("Enter Friend's Name to Remove: ");
                name.remove(input.next()); break;

            case 3:
                for(int i = 0; i < name.size(); i++)
                {
                System.out.println(name.get(i));
                }
                for(int k = 0; k < age.size(); k++)
                {    
                System.out.println(age.get(k));
                }break;
        }
        System.out.println(" 1. Add a Friend ");
        System.out.println(" 2. Remove a Friend ");
        System.out.println(" 3. Display All Friends ");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();
        }
        System.out.println("Thank you and goodbye!");
}


public String name;
public int age;

public Friends( String friendsName, int friendsAge )
{
    this.name = friendsName;
    this.age = friendsAge;
}
public String toString()
{
    return super.toString();
}
public void setName( String friendsName )
{
    name = friendsName;
} 
public void setAge( int friendsAge )
{
    age = friendsAge;
}
public String getName()
{
    return name;
}
public int getAge()
{
    return age;
}
}   

I have a few questions:

  1. How do I utilize the Friends class to store user input? (What is wrong with line 34 and 36?)
  2. When I display the friends it shows this:

    John
    Jen
    Jeff
    22
    24
    26

I'd like to have the name and age next to each other rather than all down a line. Any help would be greatly appreciated!

This is where I'm at now, but I messed something up and now it won't allow me to put an argument in "FriendsTest f = new FriendsTest();", but when I don't my friends list just says "null 0"

package friends;

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


public class FriendsTest
{
    public static void main( String[] args )
    {
        int menu;       
        int choice;
        choice = 0;      

        Scanner input = new Scanner(System.in);
        ArrayList< FriendsTest > friends = new ArrayList<  >();       

        System.out.println(" 1. Add a Friend ");
        System.out.println(" 2. Remove a Friend ");
        System.out.println(" 3. Display All Friends ");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

    while(menu != 4)
    {    

    switch(menu)
        {                     

    case 1:
                while(choice != 2)
                {

                System.out.println("Enter Friend's Name: ");
                String name = input.next();
                System.out.println("Enter Friend's Age: ");
                int age = input.nextInt();                               
                FriendsTest f = new FriendsTest(name, age);
                friends.add(f);
                System.out.println("Enter another? 1: Yes, 2: No");
            choice = input.nextInt();
        } break;

            case 2:
                System.out.println("Enter Friend's Name to Remove: ");
                friends.remove(input.next()); break;

            case 3:
                for(int i = 0; i < friends.size(); i++)
                {
                    System.out.println(friends.get(i).name + " " + friends.get(i).age);                        
                }
                break;
        }
    System.out.println(" 1. Add a Friend ");
    System.out.println(" 2. Remove a Friend ");
    System.out.println(" 3. Display All Friends ");
    System.out.println(" 4. Exit ");
    menu = input.nextInt();
    }
    System.out.println("Thank you and goodbye!");

}
public String name;
public int age;
}

Your logic is off. You never construct a Friends data structure. Also you should have one arrayList of friends and call it friends:

ArrayList<Friends> friends = new ArrayList<>();

This will store you friends data structure. The next thing you need to do is add your friends information to the friends data structure:

 while (choice != 2) {
       System.out.println("Enter Friend's Name: ");
       String name = input.next();
       System.out.println("Enter Friend's Age: ");
       String age= input.nextInt();
       Friends f = new Friends(name, age);
       friends.add(f);
       System.out.println("Enter another? 1: Yes, 2: No");
       choice = input.nextInt();
  }

Then to remove a friend, you have a slightly more complicated method where you have to iterate through the arrayList friends until you find the name of the friend then use the .remove() method.

Then to print your friends you would do:

for(int i=0;i<friends.length;i++) {
   friends.get(i).toString();
}

Your toString() method in the Friends class should probably look like this:

public void toString()
{
    System.out.println(this.getName() + " " + this.getAge());
}

assuming the name and age arrays are the same size, you could just do this:

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

also, there's no reason to store the ages and names in a seperate array, considering you have a Friends class anyway. Just store all the data in an ArrayList<Friends>

ArrayList<Friends> friendList = new ArrayList<Friends>();
string name ="";
int age ="";

            System.out.println("Enter Friend's Name: ");
            name = input.next();
            System.out.println("Enter Friend's Age: ");
            age = input.nextInt();
            friendList.Add(new Friends(name,age);  

The following doesn't compile because age has been defined of type ArrayList<Friends>

age.add(input.nextInt()); // can't add an `Integer` to a list of `Friends`

For correcting the display modify case 3 to use a single for loop with a single println() within

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

You should use one ArrayList to store all of the information. If the friend class has properties for name and age then you do not need two arraylists to store them. If you follow that approach then

name.Remove(input.next());

will remove both the name and the age. In order to display both the name and the age in one line, use only one loop to display both like this (assuming you replace names and age with one arraylist called friends):

for(Friend cFriend:friends){
    System.out.println(cFriend.getName()+"\t"+cFreind.getAge());

}

There are a few mistakes here. First off, your arraylists aren't properly parametrized and you could do the same with just one list. What you want is:

ArrayList<Friends> listOfFriends = new ArrayList<Friends>();

You then want to modify your case 1 to have add a new instance of friends to the list, so you'd write:

listOfFriends.add(new Friends(name, age));

and remove can be simplified to

listOfFriends.remove(new Friends(name, age));

For your second question, you can fix it by changing the print statement to:

for(int i = 0; i < name.size(); i++)
{
    System.out.println(listOfFriends.get(i).name + " " + listOfFriends.get(i).age);
}
break;

That should do it. Good luck!!

Java follow the concept of classes and object. What you have to do is to create a Class named Friend with required fields. Create an arraylist of Friend CLASS in a different java file or Class maybe named as ManageFriends. Also add the 4 cases under this head.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM