aspose file tools*
The moose likes Beginning Java and the fly likes Trouble with variables... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with variables..." Watch "Trouble with variables..." New topic
Author

Trouble with variables...

jason candelora
Greenhorn

Joined: Sep 25, 2003
Posts: 16
Hi,
I am not sure what is wrong with the following code, and was hoping that someone would shed some light on why I am getting the following errors when I try to compile.
newGame.java:55: cannot resolve symbol
symbol : variable number
location: class newGame
else if (test > number)
^
newGame.java:63: cannot resolve symbol
symbol : variable number
location: class newGame
else if (test < number)
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
PLEASE HELP!! (here is the code)
import javax.swing.*;
import java.text.*;
import java.util.Random;
public class newGame
{
public static void main(String[] args)
{
Random generator = new Random();
int number = generator.nextInt(1000);
String userInput = JOptionPane.showInputDialog("Give me a number between 0 and 1000");
int stringLength = userInput.length();
int guess = Integer.parseInt(userInput);
int checkedGuess = errorCheck(guess);
}
public static int errorCheck(int test)
{
if (test > 1000)
{
return 0;
}
else if (test > number)
{
return 1;
}
else if (test < number)
{
return 2;
}
else
return 3;
}
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Because you aren't passing number into the method errorCheck.
Brush up on the Java Language Basics, in particular, scope
[ October 03, 2003: Message edited by: Joe Ess ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Adding to what Joe said, A variable declared in one method is known only to that method. Thus, declaring number in main() does not make it known to errorCheck(). In other words, the scope of a variable declared in a method is limited to that method.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10


Code tags don't help if you don't format your code.
[ October 03, 2003: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
jason candelora
Greenhorn

Joined: Sep 25, 2003
Posts: 16
I am not sure what you mean..I just pasted the code in from JGrasp and tried to make it more readable for you...
I also figured out that I needed to declare my variable outside of the main method to use them in more than that one method, it seems to work ok now...
thanks for the help
Jason
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Your code with indenting included

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Trouble with variables...