# Thread: Random generate secret number into array

1. Member
Join Date
Apr 2014
Posts
3
Rep Power
0

## Random generate secret number into array

Trying to generate random integers, based off user-input for amount of integers, and then sort them into an array. The problem is that the second method needs to be int[] but I cannot figure out what to make the return result. The instructions say it needs to be an int[] in the UML diagram, so I know it's not supposed to be void.
Java Code:
```  public void generateNewSecret()
{
Random rand = new Random();{
for (int i=0; i<numDigitsSet; i++)
{
secretNumber[i]= rand.nextInt(10);
System.out.println("" + secretNumber[i]);

}
}
}

public int[] getSecretNumber()
{
for (int j=0; j<secretNumber.length; j++)
{
return System.out.println("" + secretNumber[j]);   // this line is my problem I THINK.
}

}```
Last edited by mikej911; 04-01-2014 at 10:19 PM.

3. Member
Join Date
Apr 2014
Posts
3
Rep Power
0

## Re: Random generate secret number into array

sorry, did i code tag it properly??

4. ## Re: Random generate secret number into array

Yes, you did. It looks like you're mixing the 2 methods. Let me make sure I understand what you are trying to do:

- The getSecretNumber() method should return an array of random numbers
- The generateNewSecret() method should return a single random number

Is that right?

5. Member
Join Date
Apr 2014
Posts
3
Rep Power
0

## Re: Random generate secret number into array

the getSecret method will just print out the random numbers in the array
the generateNewSecret method will generate an array of random numbers based off of what the user specified for the array length.

6. ## Re: Random generate secret number into array

For your 2nd method, if the return type must be int[], then I'm assuming that they want you to return the int[] after the values have been printed.

With that assumption, the return statement should be outside of the for loop, and return secretNumber.
Last edited by sehudson; 04-01-2014 at 11:01 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•