Hi everyone,
I am new to java and I wrote a program that does the following (using Scanner):
Name:
Last Name:
Age:
(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!
Regards,
-mike