David
David

Reputation: 14953

Whats wrong with my Random?

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.

Upvotes: 2

Views: 9881

Answers (7)

BalusC
BalusC

Reputation: 1108722

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.

Upvotes: 4

OscarRyz
OscarRyz

Reputation: 199215

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

Upvotes: 4

Betamoo
Betamoo

Reputation: 15870

Try renaming Variable Rand to other name..

Upvotes: 1

Joachim Sauer
Joachim Sauer

Reputation: 308031

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);

Upvotes: 0

David
David

Reputation: 7153

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?

Upvotes: 1

erickson
erickson

Reputation: 269667

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.

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 838216

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.

Upvotes: 5

Related Questions