2

Here's my import statement:

import java.util.*; 

Here it is in main:

 Random Rand = new Random() ; 

Here it is in a public void method :

 int a = 0 ; while (!done) { int a = Rand.nextInt(10) ; if (debug) stuff ; if (possibles[a]==1) done = true ; } 

Here's the error message i get:

 TicTacToe.java:85: cannot find symbol symbol : method nextInt(int) location: class Rand a = Rand.nextInt(10) ; ^ 

Whats going wrong here? it seems like i've done everything right to me.

3
  • 1
    The color of the Rand variable should have given you a hint.. Commented Apr 22, 2010 at 21:06
  • 2
    @Kasturi: that might be useful for experienced developers, but hardly a good hint for someone new to Java. Commented Apr 22, 2010 at 21:11
  • When you posted your code, classes are the color blue and variables would be black. Commented Apr 22, 2010 at 21:23

7 Answers 7

5

It appears that you have defined a class called Rand somewhere in your project. Don't use this for a variable name. I'd suggest using random with a small r.

Also, this line is illegal:

int a - 0 ; 

You should probably remove it as otherwise you are defining a twice.

Sign up to request clarification or add additional context in comments.

6 Comments

i searched the file with the source code and the string "rand" (not case sensitive) only apears in the code that i have quoted in the question.
nevermind you were right before i think. I have a diferent class on my comptuer called Rand.
I bet the int a - 0; is actually a typo/c&p error. That would namely have produced another compilation error before the Rand error.
@David: The updated code should still not compile. It should give the error a is already defined. That line should be removed.
@David: in the future please copypaste the actual code from your IDE into the question. Don't type over or edit afterwards, as long as you're a bit green to the stuff :) Read on about SSCCE and Smart Questions.
|
4

You defined Rand in the main method and tried to use it on your public void method. It is out of scope.

Try defining Rand in the same method ( and use lowercase this time)

Something like:

in main:

Random Rand = new Random();

In your method:

 Random rand = new Random(); int a = 0 ; while (!done) { int a = rand.nextInt(10) ; //<-- the one declared above if (debug){ stuff ; } if (possibles[a]==1){ done = true ; } } 

BTW use braces always even in 1 line if's

Comments

4
location: class Rand 

You apparently have a class named Rand in the same package or in the imports. Rename the variable name from Rand to rand according to the Sun Java Naming Conventions and it will work fix the particular problem.

2 Comments

It's a scope problem, not a shadowing problem.
@erickson: that's indeed one of many other problems to fix yet :) Do it step-by-step.
3

It sounds like Rand is a local variable in the main method.

It's not in scope when the "public void method" is invoked. So, the compiler is interpreting the identifier Rand as a class name. Coincidently, your class must be named Rand, so the compiler looks for a static method called nextInt(), and fails.

To fix it, you have a couple of options. Make the Random instance a local variable in the "public void method" that you created (by passing it as a parameter from the main method or other caller, or by instantiating within the method). Alternatively, you could declare a private static variable Rand. That variable would be in scope when invoking the method.

By the way, the convention for Java is that variables start with a lower-case letter. Types (classes, interface, and enums) start with a capital. Breaking this convention makes your code look really strange to a Java programmer.

Comments

1

The compiler told you the error was in location: class Rand.

Why does it think Rand is a class? Do you have a definition of class Rand in your code?

What happens if you call your Random object r instead of Rand?

Comments

1

Try renaming Variable Rand to other name..

Comments

0

The error message indicates that Java is looking for a method called nextInt taking an int on a class called Rand.

For some reason Java thinks that it has to look at that class instead of your variable. Is there a class called Rand in your program?

This kind of confusion can easily be avoided by following the naming conventions and starting variable names with a lower-case letter:

 Random rand = new Random(); int a = rand.nextInt(10); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.