# Help with arrays

• August 6th, 2011, 06:23 AM
mike2452
Help with arrays
Hi there
I have a scenario where a wildlife observer is looking for animals in the wild. The observer must enter a number 1-8 depending on the animal he sees. he enters the numbers one at a time and he knows which number represents each animal, which could look like this,
1
4
3
3
1
8
4
2
3
and so on
I need to make an array with 7 animals. hippo, cheetah, buffalo, monkey, zebra, tiger, snake and Rhino

I need to attach these animals to an array 1 - 8.
then I want to use the scanner tool so the user can enter the numbers in at random according to what animal he sees.
Also each animal in the array needs a counter so it can increment how many times the observer sees the particular animal. then if the user presses 0 a summary of data will come up.

• August 6th, 2011, 07:28 AM
Norm
Re: Help with arrays
Which part are you stuck with? Can you ask a more specific question?
How to get the input from the user?
How to generate random numbers in the range from 1-8?
How to build the arrays?
• August 6th, 2011, 07:40 AM
mike2452
Re: Help with arrays
Hi Norm
I have built the array
Scanner scanner = new Scanner(System.in);
Animal[0] = 1;
Animal[1] = 2;
Animal[2] = 3;
Animal[3] = 4;
Animal[4] = 5;
Animal[5] = 6;
Animal[6] = 7;
Animal[7] = 8; From this part im stuck, the numbers dont need to be generated randomly, the user will just enter the numbers in no apparent order. I dont know where the scanner goes so the inputs can look like the above.
I want to know how to code for a loop so the user inputs the numbers until the user hits "0" then the program ends. I also need to know how to code a counter for each animal so i can see how many times the animals come up

Sorry im so new to this i hope im making some sense, your help is greatly appreciated
• August 6th, 2011, 07:44 AM
Norm
Re: Help with arrays
Look at some examples of the usage of the Scanner class to read in numbers using the nextInt() method to get the input from the user.

What is the purpose of this code? Why are assigning those numbers to the array elements?
What is the animal array supposed to contain?
Code :

```animal[0] = 1; animal[1] = 2; animal[2] = 3; animal[3] = 4; animal[4] = 5;```
Variable names should start with lowercase letters.
• August 6th, 2011, 08:45 AM
mike2452
Re: Help with arrays
Hi Norm
I need the array so i can assign each animal on the array a counter variable (incrementing) so i can store the data given by the user. I will then need to show a summary of data recorded e.g what was the highest amount, lowest amount of animals seen etc. along with the totals of each animal.
So I need to figure out a code for the loop.

I hope i make sense

thanks so much
• August 6th, 2011, 09:24 AM
Norm
Re: Help with arrays
Quote:

assign each animal on the array a counter
If you are going to count, the counter's initial value should be 0.
Your code gave all the elements in the array non zero numbers.

If you only had a single variable that you were going to use to count, how would you initialize it and how would you count with it?

Now replace that single variable with an element of the array chosen by an index:
for a single variable:
Code :

``` count = 0; // initialize to 0 .... count++; // add one to the count```
for an element in the array at index ix:
Code :

``` theArray[ix] = 0; // init element to 0 .. theArray[ix]++; // add one to the count in the array at index ix```
• August 6th, 2011, 12:15 PM
JavaPF
Re: Help with arrays
Please post in the correct forum. Thread moved to - Collections and Generics