0

I am trying to write a Java class where the main method creates an array to store 30 integers. Afterwards, I call a method called LOAD() in the main method, whose job is to populate the array with 30 integers from 1-300.

I have written what I think is a complete Java class to do this, yet the compiler keeps telling me this error of that it cannot find the symbol for symbol randomThirty, my array variable's name.

Here is my code so far, yet I am not sure why my randomThirty is not being picked up by LOAD(), perhaps I need to pass the parameters explicitly in the parens of LOAD?

import java.util.*;

public class arrayNums
{

public static void main(String args[])
{
     //main method which creates an array to store 30 integers

    int[] randomThirty = new int[30]; //declare and set the array randomThirty to have 30 elements as int type

    System.out.println("Here are 30 random numbers: " + randomThirty);

LOAD(); // the job of this method is to populate this array with 30 integers in the range of 1 through 300.
}


public static void LOAD()
{
    // looping through to assign random values from 1 - 300
    for (int i = 0; i < randomThirty.length(); i++) {
        randomThirty[i] = (int)(Math.random() * 301); 
}
artemd
  • 25
  • 1
  • 4

3 Answers3

1

you need to pass randomThirty into your load method, or make it a static variable on the class (i.e. move it out of the method).

So you could do

LOAD(randomThirty); // pass the reference in

and change your method definition to

public static void LOAD(int[] randomThirty) {...}

or you can make randomThirty a static property on the class

public class arrayNums {
    // will be available anywhere in the class; you don't need to pass it around
    private static int [] RANDOM_THIRTY = new int[30]; // instantiate here
    ...

    public static void main(String args[]) { ... } // calls load
    ...
    public static void load(){...}
    ...
}

as a note, java conventions are that class names are like ArrayNums and method names should be like load or loadNums.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
1

The clue is in your comment:

LOAD(); // the job of this method is to populate this array

So you've got an array that you want the method to work with... you should pass it to the method:

load(randomThirty);

// Method declaration change...
public static void load(int[] arrayToFill)

That's a good approach when a method isn't naturally in the context where it has access to state. In this case is looks like the appropriate approach. For other situations, you could use a static or instance variable, depending on the context.

(You should look at the Random class, by the way - and learn about Java naming conventions.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • That makes sense, though when I put the randomThirty as a parameter for LOAD(); I still got this error at randomThirty.length() for the length() method. So I took out the parens at length() and that error went away, not sure why really. However, once I compiled and ran it then, the System.out.println() call to test the array now comes with this cryptic output: Here are 30 random numbers: [I@12dacd1 ...why's that? Isn't it supposed to output the array's contents (should be 30 elements of random value from 1-300). – artemd Oct 12 '11 at 16:19
  • @artemd: There isn't a length method. There's an accessible length *field* on arrays. And no, calling `toString` on an array doesn't get you the content, because the array type doesn't override `toString`. Call `Arrays.toString(array)` instead. – Jon Skeet Oct 12 '11 at 16:23
  • Ok, I get the terminology for fields there Jon Skeet. When I use the toString call, the array returned as a string is 30 values, but they're not random, just a bunch of 0s...Is there something wrong with my for loop? Or is my random just not random enough? I ran it several times, yet it keeps returning just a bunch of 0s. – artemd Oct 12 '11 at 16:30
  • @artemd: Well having fixed up your code, it works for me - so I suggest you edit your question to show what you've got now. – Jon Skeet Oct 12 '11 at 17:06
  • Ok, I'll just ask about the 0s in another question. – artemd Oct 12 '11 at 17:10
  • My question was answered here by aix, I was calling LOAD after I actually printed out to the screen with System.out: http://stackoverflow.com/questions/7743904/array-to-generate-30-random-numbers-from-1-300-returns-all-0s-instead-of-other-nu – artemd Oct 12 '11 at 17:22
0

you need to pass your array as a param , cuz it's out of the load method's scope .

LOAD(randomThirty);




public static void LOAD(int[] randomThirty)
    {
        // looping through to assign random values from 1 - 300
        for (int i = 0; i < randomThirty.length(); i++) {
            randomThirty[i] = (int)(Math.random() * 301); 
    }
Genjuro
  • 7,405
  • 7
  • 41
  • 61