tags:

views:

169

answers:

7

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.

+4  A: 
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.

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

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.

Mark Byers
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.
David
nevermind you were right before i think. I have a diferent class on my comptuer called Rand.
David
BalusC
yes its a typo i'll go change it
David
@David: The updated code should still not compile. It should give the error `a is already defined`. That line should be removed.
Mark Byers
@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](http://sscce.org) and [Smart Questions](http://catb.org/esr/faqs/smart-questions.html).
BalusC
@BalusC: My guess is that there are in fact many other errors before the Rand error and he just picked that one to post because it looked interesting.
Mark Byers
+3  A: 

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.

erickson
+1  A: 

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?

David
A: 

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);
Joachim Sauer
+3  A: 

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

OscarRyz
+1  A: 

Try renaming Variable Rand to other name..

Betamoo