Hi,
I'm having trouble with the following simple test program to extract 10 random numbers from 0 to 5.

import java.util.*;

public class TestRandom {
  public static void main(String[] args) {
    Random rand = new Random();
    for (int i = 0; i < 10; i++) {
      int j = rand.nextInt(5);
      System.out.println(j);
    }
  }
}

Compiler says: cannot find method nextInt(int);

Sorry, but I can't really see the mistake. Any help is appreciated.

Hi,
I'm having trouble with the following simple test program to extract 10 random numbers from 0 to 5.

import java.util.*;

public class TestRandom {
  public static void main(String[] args) {
    Random rand = new Random();
    for (int i = 0; i < 10; i++) {
      int j = rand.nextInt(5);
      System.out.println(j);
    }
  }
}

Compiler says: cannot find method nextInt(int);

Sorry, but I can't really see the mistake. Any help is appreciated.

I don't see any mistake either. This compiled and ran perfectly for me.

Comments
thank you very much

Thanks VernonDozier.
I don't understand then. My classpath is working fine and I can't think of any other problem.

Any ideas about where the mistake is?

Thanks

I agree with VernonDozier, your code compiled and ran OK,
You should not be getting this problem unless and until you are using a Java compiler prior to release 1.2 (i.e a pre Java2 compiler cause nextInt(int) was added to the java.util.Random class in JDK 1.2)

Comments
thanks a lot

Now it went even more strange...
The program works now for me too when using import java.util.Random; instead of import java.util.*; Anyone has seen that before, or has an idea why did that happend?

Thanks

Thanks javaAddict.
I believe you are right. I just tried to use another class under java.util (Arrays) and is working fine when using import java.util.*; .

Thanks

This question has already been answered. Start a new discussion instead.