Random.nextInt(); cannot find symbol
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.
freelancelote
Junior Poster in Training
89 posts since Aug 2008
Reputation Points: 34
Solved Threads: 2
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.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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
freelancelote
Junior Poster in Training
89 posts since Aug 2008
Reputation Points: 34
Solved Threads: 2
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)
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
Strange. I use version JDK 1.6.0_13. Just downloaded last week.
freelancelote
Junior Poster in Training
89 posts since Aug 2008
Reputation Points: 34
Solved Threads: 2
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
freelancelote
Junior Poster in Training
89 posts since Aug 2008
Reputation Points: 34
Solved Threads: 2
Do you have somewhere another class named Random?
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
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
freelancelote
Junior Poster in Training
89 posts since Aug 2008
Reputation Points: 34
Solved Threads: 2