0

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"));

        }
    }
4
  • 1
    You're creating a new array list on each loop! Commented Jun 7, 2016 at 19:11
  • you need the array outside the while Commented Jun 7, 2016 at 19:11
  • @SeekAddo I created the array list above the loop and it looks like it worked. Would there be a way for it to display on a new row after each iteration? Commented Jun 7, 2016 at 19:16
  • @jgmills yea, you need to always initialize the array outside the loop and then you can call it to add all data inside the loop Commented Jun 7, 2016 at 19:24

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Great. I am glad this helped. Happy coding.
1

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.

2 Comments

Thanks for the help but I think this is a little above my scope of Java right now. I like to do things I can understand, if that makes sense.
@jgmills Of course, If you declare the arraylist in the top (outside of the do-while loop) you will be fine.

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.