• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble with variables...

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code with indenting included

 
reply
    Bookmark Topic Watch Topic
  • New Topic