0

I've looked around for answers for this but I cannot find it. My professor requires me to use an array.. not an arraylist

public static void main(String[] args) {
    final int total = 30;
    String[] animalType = new String[total];
    Scanner input = new Scanner(System.in);

    for (int x = 0; x < total; x++) {
        System.out.println("Enter the type of animal " + x + 1);
        animalType[x] = input.next();

        for (int x1 = 0; x1 < total; x1++) {
            System.out.println("Enter the weight of the animal " + (x1 + 1));
            animalType[x1] = input.next();
        }

        input.close();

        System.out.println("Your friends are");
        for (int counter = 0; counter < total; counter++) {
            System.out.println(animalType[counter] + "\n" + animalType[counter]);
        }
    }
}

The prompt is.. allow user to enter the type of the animal and the weight of the animal and then output the average weight by animal type. I'm new to java and do not know how to use arrays properly.

4
  • In this context, input.close(); is unadvisable Commented Mar 11, 2016 at 3:29
  • So what's the exact problem? It is just to vague for a problem saying "do not know how to use arrays properly" Commented Mar 11, 2016 at 3:39
  • The "proper" way here is to create a "Animal" class, and create an Animal[]. Commented Mar 11, 2016 at 3:41
  • Could you explain further as to why I would have to create another animal class and couldn't put it all in one class? Commented Mar 11, 2016 at 3:49

2 Answers 2

1

I think you should create a class for this purpose.

First, create another file called Animal.java and write a class that stores a type and a weight:

public class Animal {
    public String type;
    public int weight;
}

Of course, it would be better if you add getters and setters, but that would be too hard for you I think. I'll show the code anyways, but I won't use it in the example below.

public class Animal {
    private String type;
    private int weight;

    public String getType() {return type;}
    public void setType(String value) {type = value;}

    public int getWeight() {return weight;}
    public void setWeight(int value) {weight = value;}
}

Now you have this class, you can create an array of it.

Animal[] animals = new Animal[total];

And you need to fill the array in with animals!

for (int i = 0 ; i < total ; i++) {
    animals[i] = new Animal();
}

Actually, your for-loops are wrong. If you want to ask the user for the type first, then the weight, you should do it like this:

for (int x = 0; x < total; x++) {
    System.out.println("Enter the type of animal " + x + 1);
    animals[x].type = input.next();
}

for (int x1 = 0; x1 < total; x1++) {
    System.out.println("Enter the weight of the animal " + (x1 + 1));
    animals[x1].weight = Integer.parseInt(input.next());
}

Now you got the types and weights of the animals, hooray!

Sign up to request clarification or add additional context in comments.

Comments

0

You need a second array to store the weight(s). Something like

String[] animalWeight = new String[total];
for(int x1 = 0; x1 < total; x1++){
    System.out.println("Enter the weight of the animal "+(x1+1));
    animalWeight[x1] = input.next();  
}

Next, you print the values from your two arrays. And I would prefer calling println twice to embedding a \n (on some systems that can also cause issues, because they use other line termination characters such as \r\n). That might look something like,

// input.close(); // <-- Also closes System.in, can cause subtle bugs.
System.out.println("Your friends are");
for(int counter = 0; counter < total; counter++){
    System.out.println(animalType[counter]);
    System.out.println(animalWeight[counter]);
}

2 Comments

Thank you for your comment. I tried something like this as a previous attempt and when I did this it started outputting "Enter the type of animal" then it would do a loop of "Enter the weight of the animal".
Yes. That is what you have programmed it to do. You could prompt for both weight and type in one loop (one after the other).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.