Hi everyone,

I am new to java and I wrote a program that does the following (using Scanner):
Last Name:
(More? Y/N):

When the user presses Y, he/she can add more information. Then in the end, I want to print something like:
The participants are: Soni Smith (30), Alma Johnson (24) ...
So, I wrote a class called "ParticipantList" that sets the values to private variables, returns their values and so on.
In the main class, I want to create an array (or list/vector) of the ParticipantList class to store multiple values:

ParticipantList[] myArray = new ParticipantList[5];
String name, lastName;
double age;

int i = 0;
char answer = 'Y';
while (answer == 'Y')
   Scanner input = new Scanner(System.in);
   System.out.print("Name: ");
   name = input.nextLine();
   System.out.print("Last Name: ");
   lastName = input.nextLine();
   System.out.print("Age: ");
   age = input.nextDouble();

   myArray[i] = new ParticipantList(name, lastName, age);
   i = i + 1;

But, what if the user types more than 5 names? 5 was the size of the Array. And what if the user types less than 5 names? In this case I get an error as well. So, how can I make it dynamic so that the array only ends when the user types 'N'? Can I create an ArrayList of my class ParticipantList? How should I declare it?

I thank you for any help you could give me!



Recommended Answers

All 4 Replies

ArrayList (and this looks familiar. Did you post this same question on Sun? Yes.)

Hi, I actually did. Is there anything wrong with that?

IMHO, not really since they are different sites (as long as you keep them all up-to-date with your progress), but many people consider even that cross-posting (I consider cross-posting to be posting on multiple forums from the site), and cross-posting is considered extremely rude. Since "Tom" on site "abc" could take time out of his day to try and help you with the problem, only to find out the "Joe" on site "xyz" (or the same site) already provided all the same pointers (and possibly even completely solved your problem) two days ago, meaning "Tom" has just completely wasted that time of his because you cross-posted a question and didn't even bother to keep them up-to-date with each other.

Edit: Just like Sabre has just given you the necessary pointers at Sun, which you didn't bother here to point out what they were or how (or if) they helped you, so someone here may waste their time reiterating them.

I apologize for any inconvenience! And I am closing this thread here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.