0

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

3
  • have you tried System.out.println(animalList); outside of the for loop? Commented Jun 5, 2020 at 4:51
  • You want to add just the names to AnimalList? You've already got code that does that (animalList.add(mInputn);). Commented Jun 5, 2020 at 5:02
  • 1
    @BookOfZeus yep, that was it, thank you for your help, I was up all day working on it by the end of the day my brain was missing the most crucial piece.
    – Alita
    Commented Jun 5, 2020 at 14:55

2 Answers 2

0

Well if your just going to do System.out.println(animalList); then you are just going to get a memory location and not the actaual things inside this list; so what I personally do is after the user inputs and completes the loop write this code out side the first for loop. It is a for each loop: (delete the last System.out.println();)

for(String i : animalList){
System.out.println(i);
}

Or you could also do:

for(int i = 0; i < animalList.length; i++){
System.out.println(animalList[i]);
}

And also after your finish printing out the animal's info do:

 System.out.println("Acquisition Date: " + animal[i].getAcquisitionDate() + "\n);

Instead of:

System.out.println("Acquisition Date: " + animal[i].getAcquisitionDate());
System.out.println();
0

First the code. Explanations after the code.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class RescueAnimal {
    private String name;
    private String type;
    private String gender;
    private int age;
    private double weight;
    private String acquisitionDate;

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public String getGender() {
        return gender;
    }

    public int getAge() {
        return age;
    }

    public double getWeight() {
        return weight;
    }

    public String getAcquisitionDate() {
        return acquisitionDate;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public void setAcquisitionDate(String acquisitionDate) {
        this.acquisitionDate = acquisitionDate;
    }

    public static void main(String[] args) {
        List<RescueAnimal> animalList = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        System.out.println("Would you like to add an animal? Y or N ");
        char choice = input.nextLine().toUpperCase().charAt(0);
        if (choice == 'Y')  {
            System.out.println("How many animals would you like to add?");
            int choice2 = input.nextInt();
            input.nextLine();
            for (int i = 0; i < choice2; ++i)  {
                System.out.println("Enter new animal name: ");
                String mInputN = input.nextLine();

                System.out.println("Enter type of animal: ");
                String mInputT = input.nextLine();

                System.out.println("Enter gender of animal: ");
                String mInputG = input.nextLine();

                System.out.println("Enter age of animal: ");
                int mInputA = input.nextInt();
                input.nextLine();

                System.out.println("Enter weight of animal: ");
                double mInputW = input.nextDouble();
                input.nextLine();

                System.out.println("Date acquired (DD/MM/YYYY");
                String mInputD = input.nextLine();

                RescueAnimal animal = new RescueAnimal();
                animal.setName(mInputN);
                animal.setType(mInputT);
                animal.setGender(mInputG);
                animal.setAge(mInputA);
                animal.setWeight(mInputW);
                animal.setAcquisitionDate(mInputD);

                animalList.add(animal);

                System.out.println(animal.getName() + ", Age: " + animal.getAge() + ", Gender: " + animal.getGender());
                System.out.print("Type: " + animal.getType() + ", ");
                System.out.print("Weight: " + animal.getWeight() + ", ");
                System.out.println("Acquisition Date: " + animal.getAcquisitionDate());
                System.out.println();

                System.out.println(animalList.size());
            }
        }
    }
}

You want to create a list of RescueAnimal and not a list of String.
You have not declared any arrays so you don't need [i]. An ArrayList is not an array.
All you want to do is create an instance of RescueAnimal from the details entered by the user and then add that RescueAnimal instance to your list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.