-1

i am trying to call a method to generate a random number, but Java won't let me. It is telling me to create a method even though i already have. Here is my code.

package hw3;

import java.util.Random;

//Zephram Foster, Homework 3, 10-12

public class randomLottery {

    int num[];
    int ball;
    int high;


    public static int generateRandomInt(int upperRange){

        Random random = new Random();
        return random.nextInt(upperRange);
    }

}


package hw3;
import java.util.Scanner;
public class randomMain {

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the high number: ");
        //int high = sc.nextInt();
        //System.out.println(high);

        randomLottery rand = new randomLottery();
        rand.high = sc.nextInt();
        System.out.println(rand.high);

        rand.num[1] = generateRandomInt(rand.high);
    }

}

the problem is in the main class, the last line. Please help my dudes

1 Answers1

3

The generateRandomInt() method is in another class. Given that this method also happens to be static, you may refer to it as:

rand.num = new int[2];
rand.num[1] = randomLottery.generateRandomInt(rand.high);

Note that Java naming conventions say that class names should begin with a capital letter, so call the other class RandomLottery.

I don't know what the rest of your not yet written code would look like. Java arrays begin an index zero, so the following assignment might make more sense:

rand.num[0] = randomLottery.generateRandomInt(rand.high);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360