Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Help with arrays

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

    Im so stuck on this can anyone please help??


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    animal[0] = 1;
    animal[1] = 2;
    animal[2] = 3;
    animal[3] = 4;
    animal[4] = 5;
    Variable names should start with lowercase letters.
    Last edited by Norm; August 6th, 2011 at 07:46 AM.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with arrays

    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:
      count = 0;   // initialize to 0
      ....
      count++;    // add one to the count
    for an element in the array at index ix:
      theArray[ix] = 0; // init element to 0
      ..
      theArray[ix]++; // add one to the count in the array at index ix
    Last edited by Norm; August 6th, 2011 at 09:26 AM.

  7. The Following User Says Thank You to Norm For This Useful Post:

    mike2452 (August 6th, 2011)

  8. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help with arrays

    Please post in the correct forum. Thread moved to - Collections and Generics
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Arrays
    By av8 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 25th, 2011, 01:21 PM
  2. 3 Arrays
    By D3158 in forum Collections and Generics
    Replies: 27
    Last Post: June 16th, 2011, 11:10 AM
  3. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  4. arrays
    By dwamalwa in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2010, 01:44 AM
  5. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM