Unable to read .next() in Java



I am unable to compile the source code of Random.java, because on an error I get when I compile it.

Random.java:10: error: cannot find symbol
randomNum = rng.nextInt();
symbol: method nextInt()
location: variable rng of type Random
1 error

The Full Text of Random.java

import java.util.;
public class Random {
public static void main(String[] args) {
int x = 0;
int randomNum;
do {
Random rng = new Random();
randomNum = rng.nextInt();
System.out.println("The number is " + randomNum + “!\n”);
System.out.print("If a coin is fliped, ");
if (randomNum > 0)
System.out.println(“it’s heads\n”);
if (randomNum <= 0)
System.out.println(“it’s tails\n”);
} while (x < 5);

It works fine inside of any IDE other than this one.

Someone please help.


Your class is called Random and there is a built-in class called Random. So when you write “new Random()” it’s referring to yours and not the built-in one that you want.

To avoid the problem, rename yours to something other than Random.


Whoops, I totally forgot about that! Thanks, it works fine now.:slight_smile: