0

I've written a small Java app to generate arrays randomly, with random numbers. This class works properly:

Dice.java

import java.util.*;

public class Dice {
    private int i = 10;
    private int value = 0;
    private Random rnd = new Random();

    public void roll_dice() {
        int g = i;

        value = rnd.nextInt(g); // Here is not make a  single error.
        if ((value != 0) & (value <= 6)) {
            System.out.println("Number is :" + value);
        } else {
            System.out.println(" Error number should be between 1 and 6. Try again");
        }
    }
}

When I go to compile this following class, however, I get the following error:

Error cannot find symbol - method nextInt(int)

Random.java

import java.util.*;

public class Random {
    private int[] number;
    private int g;
    private Random rnd = new Random();

    public void Bicbig(int amount) {
        g = amount;

        if ((amount >= 1) & (amount <= 100)) {

        } else {
            System.out.println("Please Enter Between 1 and 100. Please try again .");
        }
    }

    public void generate() {
        number = new int[g];

        int u = rnd.nextInt(g); // Error cannot find symbol - method nextInt(int)
    }
}

Why am I getting this exception? How can I fix it?

4 Answers 4

3

You shouldn't name your class as Random. It hides java.util.Random. Program will compile after renaming

Sign up to request clarification or add additional context in comments.

Comments

1

You called your class Random, overriding java's Random class. Rename it.

The nextInt() method doesnt exist in your random class

Comments

1

You have a name clash between your Random function, and the one in java.util.

  1. Remove import java.util.*; and refer to the Java library function as java.util.Random.

  2. Rename your Random class.

In reality, I'd do both.

Comments

1

Change

private Random rnd = new Random() ;

to

private java.util.Random rnd = new java.util.Random();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.