I am working on a Java project and I have my Super class with private attributes. I use the accessors and mutators I inherited from my superclass inside Main to set and get these attributes. I am creating a for loop to iterate through the inputs based on the INT number of the user. I am stuck on how do I take my user inputs of the Name and add it to an ArrayList inside the loop, so when it is done I should have X number of names.
Scanner input = new Scanner(System.in);
ArrayList<String> animalList = new ArrayList<String>();
//local variables
char choice;
int choice2;
String mInputN;
String mInputT;
String mInputG;
int mInputA;
double mInputW;
String mInputD;
//user input if else statement for Y or N
System.out.println("Would you like to add an animal? Y or N ");
choice = input.next().toUpperCase().charAt(0);
if (choice == 'Y') {
System.out.println("How many animals would you like to add?");
choice2 = input.nextInt();
for (i = 0; i < choice2; ++i) {
System.out.println("Enter new animal name: ");
mInputN = input.next();
System.out.println("Enter type of animal: ");
mInputT = input.next();
System.out.println("Enter gender of animal: ");
mInputG = input.next();
System.out.println("Enter age of animal: ");
mInputA = input.nextInt();
System.out.println("Enter weight of animal: ");
mInputW = input.nextDouble();
System.out.println("Date acquired (DD/MM/YYYY");
mInputD = input.next();
RescueAnimal animal[i] = new RescueAnimal();
animal[i].setName(mInputN);
animal[i].setType(mInputT);
animal[i].setGender(mInputG);
animal[i].setAge(mInputA);
animal[i].setWeight(mInputW);
animal[i].setAcquisitionDate(mInputD);
animalList.add(mInputN);
System.out.println(animal[i].getName() + ", Age: " + animal[i].getAge() + ", Gender: " + animal[i].getGender());
System.out.print("Type: " + animal[i].getType() + ", ");
System.out.print("Weight: " + animal[i].getWeight() + ", ");
System.out.println("Acquisition Date: " + animal[i].getAcquisitionDate());
System.out.println();
System.out.println(animalList);
}
System.out.println(animalList);
outside of the for loop?AnimalList
? You've already got code that does that (animalList.add(mInputn);
).