• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with variables...

 
jason candelora
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9297
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Code tags don't help if you don't format your code.
[ October 03, 2003: Message edited by: Marilyn de Queiroz ]
 
jason candelora
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code with indenting included

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic