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.

Recommended Answers

All 7 Replies

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.

commented: thank you very much +1

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)

commented: thanks a lot +1

Strange. I use version JDK 1.6.0_13. Just downloaded last week.

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

Do you have somewhere another class named Random?

commented: thanks a bunch +1

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.