Results 1 to 6 of 6
  1. #1
    mikej911 is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default 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 11:19 PM.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default Re: Random generate secret number into array

    You have a few issues with your code, and we can help you out, but please put CODE tags around your code, it makes it easier to read.

  3. #3
    mikej911 is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: Random generate secret number into array

    sorry, did i code tag it properly??

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default 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. #5
    mikej911 is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default 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. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default 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-02-2014 at 12:01 AM.

Similar Threads

  1. Replies: 2
    Last Post: 03-11-2013, 07:12 PM
  2. Generate Random Number Loop
    By saber210 in forum New To Java
    Replies: 9
    Last Post: 08-12-2011, 05:49 PM
  3. Replies: 3
    Last Post: 10-24-2010, 11:24 PM
  4. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 09:37 AM
  5. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM

Posting Permissions

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