0

I'd like to know why I got these errors and how to fix it:

Main.java:12: error: '.class' expected
        populateArray(int[] aRand);
                            ^
Main.java:12: error: ';' expected
        populateArray(int[] aRand);

The instructions I was given are in the comments and I just followed what I believe what the instructions meant. I thought maybe I should do populateArray(); in the main method, but it wouldn't match the signature of public void populateArray(int[] array){}.

import java.util.Random;

public class Main {
    public static void main(String args[])
    {
    /*
    (a) Create an array of integers called aRand of capacity 20 and initialize its entries to 0. 
    */
        int aRand[] = new int[20];

        populateArray(int[] aRand);

    }

    /*
    (b) Populate the array in a method called populateArray(int[] array) where array is the one you want to populate. 
    This method returns void. 
    */
        public void populateArray(int[] array){
          Random rand = new Random();
          for (int i=0; i<array.length; i++){
            array[i] = rand.nextInt();
            System.out.println(array[i]);
          }
        }
}
Jack Ryan
  • 1,287
  • 12
  • 26

1 Answers1

0

Just a couple small things!

First, you already declared populateArray as a method that takes an int[], so you do not need to do so when you actually call the method.

Second, you cannot call a non-static method from a static context. Since your main method is static, you cannot call the non-static method populateArray within it. For your purposes, it should be fine to just declare populateArray as a static method.

import java.util.Random;

public class Main {
    public static void main(String args[])
    {
    /*
    (a) Create an array of integers called aRand of capacity 20 and initialize its entries to 0. 
    */
        int aRand[] = new int[20];

        populateArray(aRand); // We already know aRand is an int[]

    }

    /*
    (b) Populate the array in a method called populateArray(int[] array) where array is the one you want to populate. 
    This method returns void. 
    */
    public static void populateArray(int[] array){
        Random rand = new Random();
        for (int i=0; i<array.length; i++){
            array[i] = rand.nextInt();
            System.out.println(array[i]);
        }
    }
}

For reference, here as a helpful StackOverflow answer that discusses when you should use static vs. instance methods: Java: when to use static methods.

Jack Ryan
  • 1,287
  • 12
  • 26
  • Thank you! I thought populateArray(aRand); had to exactly match the signature of the void method. As for the static part, yes-- that's what I was confused on too! I'm a little confused as to what they're saying in that link but it sounds like Obj.someMethod means the method is non-static. If you're calling the method with someMethod(), then that method is static? I will look up more info. –  Jun 02 '19 at 04:13
  • Well, it _does_ have to exactly match the signature, but it already does. The line `int aRand[] = new int[20];` tells us that `aRand` is already an integer array, so you're all set from then on just referring to it as `aRand`. – Jack Ryan Jun 02 '19 at 04:24