Question

public static int[] getLottoNumberGenerator()
{
    int[] numberGenerator = new int[9];
    {
        numberGenerator[0] = (int) ((9 * Math.random()) +1);
        numberGenerator[1] = (int) ((9 * Math.random()) +1);
        numberGenerator[2] = (int) ((9 * Math.random()) +1);
        numberGenerator[3] = (int) ((9 * Math.random()) +1);
        numberGenerator[4] = (int) ((9 * Math.random()) +1);
        numberGenerator[5] = (int) ((9 * Math.random()) +1);
        numberGenerator[6] = (int) ((9 * Math.random()) +1);
        numberGenerator[7] = (int) ((9 * Math.random()) +1);
        numberGenerator[8] = (int) ((9 * Math.random()) +1);
    }
    return numberGenerator;

When I return numberGenerator, I get a series of letters, numbers and other characters.

How can I edit my code to return 9 (int) to the main method? I already tried return numberGenerator[0] but it doesn't work.

Thanks in advance.

Was it helpful?

Solution

It is returning your array with valid data in it. (Although I question the necessity of the curly braces after you instantiate your array, in which you populate the data.)

What you're likely doing is printing out the result of the call:

System.out.println(getLottoNumberGenerator());

That's not going to work; it'll print out the array's default toString, which would look something like [I@827328. Not helpful at all.

What you want to do instead is wrap that in a call to Arrays.toString().

System.out.println(Arrays.toString(getLottoNumberGenerator());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top