0

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.

4
Contributors
7
Replies
9
Views
7 Years
Discussion Span
Last Post by freelancelote
Featured Replies
  • [QUOTE=freelancelote;886630]Hi, I'm having trouble with the following simple test program to extract 10 random numbers from 0 to 5. [CODE=java] 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 = … Read More

  • 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) Read More

  • Do you have somewhere another class named Random? Read More

1

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
0

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

1

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
0

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

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.