Request for help to tidy up code involving arrays and loops I posted the following code on here a few days ago:

public class Practical4_Assessed
{
    public static void main(String[] args)
    {

        Random numberGenerator = new Random();
        int[] arrayOfGenerator = new int[100];
        int[] countOfArray = new int[10];
        int count;

        for (int countOfGenerator = 0; countOfGenerator &lt; 100; countOfGenerator++)
        {
            count = numberGenerator.nextInt(10);
            countOfArray[count]++;
            arrayOfGenerator[countOfGenerator] = count + 1;
        }

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

        System.out.println("");
        System.out.println("");

        for (int countOfNumbers = 0; countOfNumbers &lt; countOfArray.length; countOfNumbers++)
            System.out.println("The number " + (countOfNumbers + 1)
                    + " occurs " + countOfArray[countOfNumbers] + " times.");

        System.out.println("");

        for (int countOfNumbers = 0; countOfNumbers &lt; countOfArray.length; countOfNumbers++)
        {
            if (countOfNumbers != 9)
                System.out.print((countOfNumbers + 1) + "   ");
            else
                System.out.print((countOfNumbers + 1) + "  ");

            for (int a = 0; a &lt; countOfArray[countOfNumbers]; a++)
            {
                System.out.print("*");
            }
            System.out.println("");
        }

        System.out.println("");

        int max = 0;
        int test = 0;
        for (int counter = 0; counter &lt; countOfArray.length; counter++)
        {
            if (countOfArray[counter] &gt; max)
            {
                max = countOfArray[counter];
                test = counter + 1;
            }
        }

        System.out.println("The number that appears the most is " + test);

    }
}


I posted it for help on how to tidy it up - the suggestion I got was to add methods to break the code down into more manageable, testable sections.
However, despite spending the best part of two days trying, I can't get my head around how to do this. Everytime I try, I either generate compile-time or run-time errors.

Would anyone mind showing me even one or two of the methods I could use, so I can work from that?
After stressing about it for about two hours, I got the methods sorted, so I'll answer my own question. Please note - I'm just a beginner and I'm sure there are plenty of things that could be done even more efficiently!

import java.util.Random;
// Imports Java random number generator.

public class Practical4_Assessed_with_Methods 
{
static int[] randomNumbers = new int[100];
static int[] countNumbers = new int[10];
// Two arrays declared, one to hold 100 elements, the other 10.

public static void main(String[] args) 
{
    programExplanation();
    generateNumbers(randomNumbers, countNumbers);
    printNumbers(randomNumbers);
    generateNumberOfOccurences(countNumbers);
    generateAsteriskPattern(countNumbers);
    findMaxValues(countNumbers);
    // Code is broken down into five method calls (to make each section more manageable and testable).
}

static void programExplanation()
{
    System.out.println("This program will generate 100 random integers between 1 and 10. " +
            "It will then output them in rows of 10.");
    System.out.println("Following this, it will output how often each integer occured in " +
            "the array.");
    System.out.println("It will also output this information in a horizontal bar chart of " +
            "asterisks.");
    System.out.println("Finally, it will output which number, or numbers appeared most often");
    System.out.println("");
}

static void generateNumbers(int[] arrayOfGenerator, int[] countArray)
{
    Random numberGenerator = new Random ();
    for (int countOfGenerator = 0; countOfGenerator &lt; 100; countOfGenerator++) // For loop continues 100 times.
    {
        int count = numberGenerator.nextInt(10);
        countArray[count]++;
        arrayOfGenerator[countOfGenerator] = count + 1;
    }
    /*  A random number (from 0 and 9) is generated and stored in count.
     *  Whatever number is generated, that element number of countArray is incremented by 1.
     *  E.g. If random number generated = 4, fourth element of countArray (i.e. element 3) is increased by 1.
     *  The element of arrayOfGenerator (determined by the loop) is given the value of count + 1.
     *  This is because the numberGenerator creates integers from 0 and 9 - we want from 1 and 10.

     *  This coding is beneficial as both arrays are filled at the same time, which saves processing time.
     */
}

static void printNumbers(int[] arrayOfGenerator)
{
    int countOfNumbersOnLine = 0;
    for (int countOfOutput = 0; countOfOutput &lt; 100; countOfOutput++)
    {
        if (countOfNumbersOnLine % 10 == 0)
            System.out.println("");
        //  Takes a new line when 10 integers have been printed on a line.

            if (arrayOfGenerator[countOfOutput] == 10)
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "  ");
                countOfNumbersOnLine++;
            }
            else
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "   ");
                countOfNumbersOnLine++;
            }

    }
    /*  Used to calculate the amount of integers on a line and the spacing between them.
     *  If the arrayOfGenerator is 10, it takes two spaces, else it takes 3 spaces (to ensure proper spacing).
     *  This is needed because the numeral 10 has two characters, as opposed to 1-9.
     */

    System.out.println("");
    System.out.println("");
}

static void generateNumberOfOccurences(int[] countOfArray)
{
    for (int countOfNumbers = 0; countOfNumbers &lt; countOfArray.length; countOfNumbers++)
        System.out.println("The number " + (countOfNumbers + 1) + " occurs " + countOfArray[countOfNumbers] + " times.");
    System.out.println("");
    /*  For loop continues for entire length of countOfArray (i.e. 10 times)
     *  Prints out how often each random number occured.
     */
}

static void generateAsteriskPattern(int[] countOfArray)
{
    for (int countOfNumbers = 0; countOfNumbers &lt; countOfArray.length; countOfNumbers++)
    {
        if (countOfNumbers != 9)
            System.out.print((countOfNumbers + 1) + "   ");
        else
            System.out.print((countOfNumbers + 1) + "  ");

        for (int asteriskCount = 0; asteriskCount &lt; countOfArray[countOfNumbers]; asteriskCount++)
            System.out.print("*");
        System.out.println("");
    }
    System.out.println("");
    /*  For loop continues for entire length of countOfArray (i.e. 10 times)
     *  First if/else statement determines how many spaces are taken after numeral and before asterisks.
     *  This is used to ensure all asterisks are vertically aligned when output.
     *  Second for statement runs on each element of countOfArray, outputting asterisks for as long as array lasts. 
     *  For example, if element 0 has the value 3, 3 asterisks are output. 
     */
}

static void findMaxValues(int[] countOfArray)
{
    int max = 0;
    for (int counter = 0; counter &lt; countOfArray.length; counter++)
    {
        if (countOfArray[counter] &gt; max)
        {
            max = countOfArray[counter];
        }
    }

    System.out.print("Number(s) that appears the most:");
    /*  For loop continues for entire length of countOfArray (i.e. 10 times) and compares each value to max.
     *  If value compared is larger than the max value, it becomes new max value.
     *  By the end of this loop, largest value in the array is stored in max. 
     */

    for(int checkValue = 0; checkValue &lt; countOfArray.length; checkValue++)
        if(countOfArray[checkValue] == max)
            System.out.print(" " + (checkValue + 1));
    System.out.print(".");
    /*  For loop continues for entire length of countOfArray (i.e. 10 times)
     *  If value of element scanned is equal to the value in max, it is output. 
     */
}
}


Andrew