How to scan an array for certain information

Posted by Andrew Martin on Stack Overflow See other posts from Stack Overflow or by Andrew Martin
Published on 2012-10-27T10:56:49Z Indexed on 2012/10/27 11:00 UTC
Read the original article Hit count: 353

Filed under:
|

I've been doing an MSc Software Development conversion course, the main language of which is Java, since the end of September. We have our first assessed practical coming and I was hoping for some guidance.

We have to create an array that will store 100 integers (all of which are between 1 and 10), which are generated by a random number generator, and then print out ten numbers of this array per line. For the second part, we need to scan these integers, count up how often each number appears and store the results in a second array.

I've done the first bit okay, but I'm confused about how to do the second. I have been looking through the scanner class to see if it has any methods which I could use, but I don't see any. Could anyone point me in the right direction - not the answer, but perhaps which library it comes from?

Code so far:

import java.util.Random;

public class Practical4_Assessed 
{

public static void main(String[] args) 
{

    Random numberGenerator = new Random ();

    int[] arrayOfGenerator = new int[100];

    for (int countOfGenerator = 0; countOfGenerator < 100; countOfGenerator++)
        arrayOfGenerator[countOfGenerator] = numberGenerator.nextInt(10);

    int countOfNumbersOnLine = 0;
    for (int countOfOutput = 0; countOfOutput < 100; countOfOutput++)
    {
        if (countOfNumbersOnLine == 10)
        {
            System.out.println("");
            countOfNumbersOnLine = 0;
            countOfOutput--;
        }
        else
        {
            System.out.print(arrayOfGenerator[countOfOutput] + " ");
            countOfNumbersOnLine++;
        }
    }
  }
}

Thanks, Andrew

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays