jgmills
jgmills

Reputation: 3

Repeat user inputs while adding it to an array list

I’m trying to ask the user if they want to add another person to array list, "list". My thought was to loop to ask, then add those inputs into an array, "inf" and then put that array within an array list. Right now when I repeat the code it just erases the previous inputs; how would I make it so it adds to the array list after each iteration?

public class test {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner scan = new Scanner(System.in);

        String ask = "no";

        do {
            System.out.println("Add? (yes/no)");
            ask = scan.nextLine();
            if (ask.equals("yes")){

                //gather user info
                System.out.println("Last Name: ");
                String LastN = scan.nextLine();


                System.out.println("First Name: ");
                String FirstN = scan.nextLine();


                System.out.println("# of Children:");
                String Children = scan.nextLine();


                System.out.println("Address:");
                String Adr = scan.nextLine();

                System.out.println("Phone #:");
                String Pho = scan.nextLine();

                Date dategather = new Date();   
                String Date = String.format("%tD", dategather);

                //Input the data into an array
                String[] inf = {LastN,FirstN,Children,Adr,Date,Pho};

                ArrayList<String> list = new ArrayList<String>();

                Collections.addAll(list,inf);

                System.out.println(list);               

            }

            } while (ask.equals("yes"));

        }
    }

Upvotes: 0

Views: 103

Answers (2)

ishmaelMakitla
ishmaelMakitla

Reputation: 3812

You'd want to declare the list outside the do-while structure, and if you want to print each line entry - then you do that right at the end of the do-while structure:

 //here you declare the list OUTSIDE:

 ArrayList<String> list = new ArrayList<String>();

  do {
            System.out.println("Add? (yes/no)");
            ask = scan.nextLine();
            if (ask.equals("yes")){

                //gather user info
                System.out.println("Last Name: ");
                String LastN = scan.nextLine();

                System.out.println("First Name: ");
                String FirstN = scan.nextLine();

                System.out.println("# of Children:");
                String Children = scan.nextLine();

                System.out.println("Address:");
                String Adr = scan.nextLine();

                System.out.println("Phone #:");
                String Pho = scan.nextLine();

                Date dategather = new Date();   
                String Date = String.format("%tD", dategather);

                //Input the data into an array
                String[] inf = {LastN,FirstN,Children,Adr,Date,Pho};

                Collections.addAll(list,inf);
                //so here you want to display what the person just entered:
                System.out.println("You Have Entered : \n Last Name: "+LastN+"  First Name: "+FirstN+" Children : "+Children+" Address: "+Adr+" Date of Birth: "+Date+" Phone Number: "+Pho);               

            }

            } while (ask.equals("yes"));

I hope this helps you. Please give it a try and let me know.

Upvotes: 1

Dionysis Nt.
Dionysis Nt.

Reputation: 955

You can create a class Person which has all the details of each person and add them to an ArrayList. Something like that: (tip: you could make #children as integer instead of String)

public class test
{
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Person> list = new ArrayList<Person>();

        do
        {
            System.out.println("Add? (yes/no)");
            ask = scan.nextLine();
            if (ask.equals("yes")){
                   System.out.println("Last Name: ");
                    String lastName= scan.nextLine();


                    System.out.println("First Name: ");
                    String firstName= scan.nextLine();


                    System.out.println("# of Children:");
                    String children = scan.nextLine();


                    System.out.println("Address:");
                    String address= scan.nextLine();

                    System.out.println("Phone #:");
                    String phone= scan.nextLine();

                    Date dategather = new Date();   
                    String date = String.format("%tD", dategather);
                    list.add(new Person(lastName, firstName, children,address, phone,date));


            }
        }while (ask.equals("yes"));
    }

}

public class Person
{
    String lastName, firstName, children,address, phone,date
    public Person(String lastName,String firsName,String children,String address,String phone, String date)
    {
        this.lastName=lastName;
        this.firsName=firsName;
        this.children=children;
        this.address=address;
        this.phone=phone;
        this.date=date;
    }
}

In your code in every loop the arrayList is reseting as you reinitialize every time.

Upvotes: 1

Related Questions