#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Using Arrays in Methods/Constructors


    I'm not sure how to fill an array with random numbers, this is my go at it.

    private int[] lotterynumbers = new int[5];

    public int randomNumbers(){
    for(int val: lotterynumbers){
    Random number = new Random();
    lotterynumbers[val] = number.nextInt(9);
    }
    return lotterynumbers[val];
    }

    My second problem is with the constructor. Whenever i try to create an object for this class it says that it's undefined.

    public MashackLottery(int l[]){
    for (int value: lotterynumbers){
    lotterynumbers[value] = l[value];
    }
    }

    MashackLottery picked = new MashackLottery();
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by NotGoodAtThisYo
    I'm not sure how to fill an array with random numbers, this is my go at it.

    private int[] lotterynumbers = new int[5];

    public int randomNumbers(){
    for(int val: lotterynumbers){
    Random number = new Random();
    lotterynumbers[val] = number.nextInt(9);
    }
    return lotterynumbers[val];
    }

    My second problem is with the constructor. Whenever i try to create an object for this class it says that it's undefined.

    public MashackLottery(int l[]){
    for (int value: lotterynumbers){
    lotterynumbers[value] = l[value];
    }
    }

    MashackLottery picked = new MashackLottery();
    In the code, you are trying to call a no-argument constructor instead of the one you defined.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by bullet
    In the code, you are trying to call a no-argument constructor instead of the one you defined.
    This part of the code is in a separate tester program:

    MashackLottery picked = new MashackLottery();

    I just want to be able to use methods from the class in my tester program. How would I do that?
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by NotGoodAtThisYo
    This part of the code is in a separate tester program:

    MashackLottery picked = new MashackLottery();

    I just want to be able to use methods from the class in my tester program. How would I do that?
    If you want to use that code, you'll need to add a no-argument constructor.

IMN logo majestic logo threadwatch logo seochat tools logo