-1
import java.util.*;

public class ChristmasParty
{
    private Scanner input = new Scanner(System.in);

    public static void main(String[] args)
    {
        ChristmasParty cp = new ChristmasParty();
        cp.run();
    }

    public void run()
    {
        menu();
        addName();
    }

    public void menu()
    {
        System.out.println("Welcome to the guests lists program");
        System.out.println("Enter a name or enter X to quit");
    }

    public void addName()
    {
        String nameAdded = "";
        ArrayList<String> guestsLists = new ArrayList<String>();
        do
        {
            System.out.print("Enter a name: ");
            nameAdded = input.nextLine();

            guestsLists.add(nameAdded);
            System.out.println("-----------------------------------------------");
            System.out.println(nameAdded + " has been added to the guests list.");

        }while(!nameAdded.equals("X"));
        System.out.println("Guests lists:");
        System.out.println("========================");
        System.out.println(guestsLists.get(0));
        System.out.println(guestsLists.get(1));
        System.out.println(guestsLists.get(2));
        System.out.println(guestsLists.get(3));
        System.out.println(guestsLists.get(4));
        System.out.println(guestsLists.get(5));
        System.out.println(guestsLists.get(6));
        System.out.println(guestsLists.get(7));
        System.out.println(guestsLists.get(8));
        System.out.println(guestsLists.get(9));
        System.out.println(guestsLists.get(10));
        System.out.println(guestsLists.get(11));
        System.out.println(guestsLists.get(12));
        System.out.println(guestsLists.get(13));
        System.out.println(guestsLists.get(14));
        System.out.println(guestsLists.get(15));
    }
}  

Hello, I am trying to make a code that will prompt the user to enter a name,and that name will be saved in the arraylists, and then once the user exits and quits by pressing "X", it will display all the names in the lists. However how do I make the System.out.println(guestsLists.get()); codes simpler rather than typing it all out?

2 Answers2

4

Put this code:

}while(!nameAdded.equals("X"));

System.out.println("Guests lists:");
System.out.println("========================");

for(int i = 0; i < guestsLists.size(); i++)
{
    System.out.println(guestsLists.get(i));
}

OR

for(String s : guestsLists)
{
    System.out.println(s);
}

And remove all the sysouts after the

System.out.println("========================");

Rajnikant Patel
  • 561
  • 3
  • 19
3

Simply iterate through the list using an enhanced for loop:

...
for (String name : guestsLists) {
    System.err.println(name);
}
...

You can use that form of a for loop when you do not need an index inside the loop, but just want to iterate over the collection. In these cases, it is much easier to read (and less code) than using an explicit index (or using an explicit Iterator).

See also https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123