Results 1 to 5 of 5
Thread: Cannot find symbol variable
- 11-28-2011, 06:38 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Cannot find symbol variable
I really need help with this pls I got two program and i must combine them together
This is the method in the class :
int length(int p){
int chars;
String q = Integer.toString(p);
return chars = q.length();
}
Now in the main i got a problem about the error: Cannot find symbol variable chars
The main program goes like this:
public class Numbers{
public static void main (String args[]) {
NumberUtils nu = new NumberUtils();
System.out.println("Enter:::");
int p = Keyboard.readInt();
nu.length(p);
System.out.println("Number of characters= " + chars);
}
}
I am a beginner with java and i need this for tomorrow thx for your time and help
Drew
- 11-28-2011, 06:53 PM #2
Re: Cannot find symbol variable
The scope of chars is only inside your length() method. You can't access it outside of that method.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
Re: Cannot find symbol variable
You'll want to read up on scope which is the visibility of variables and methods in Java. Your chars variable is declared inside of the length method and thus is visible only inside of this method. If it needs to be visible outside, then you'll want to declare it in the class, not in the method. You'll also want to then give the class a public getter method for this property/variable.
- 11-28-2011, 07:05 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: Cannot find symbol variable
Thanks very much both . I figured it out i have some problem in the class because like you both said i couldn't do it outside of the method. Thanks a lot both
-.gif)
- 11-28-2011, 07:09 PM #5
Re: Cannot find symbol variable
Also, that method is returning a value (which is the value of chars, incidentally), which you seem to be throwing away.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
cannot find symbol variable lines 24, 25, 44, 45, 63, 64? Help!
By ironman_777 in forum New To JavaReplies: 4Last Post: 12-25-2010, 01:17 PM -
Cannot find symbol variable Keyboard...
By Alnegar in forum New To JavaReplies: 6Last Post: 10-30-2010, 05:39 PM -
Cannot find symbol variable - Why? I can.. ^^
By Mattedatten in forum New To JavaReplies: 4Last Post: 03-08-2010, 07:07 PM -
Cannot find symbol variable pD! I cant fix it!!!
By Addez in forum New To JavaReplies: 2Last Post: 09-17-2009, 08:32 PM -
Cannot find symbol variable yourScore
By Addez in forum New To JavaReplies: 4Last Post: 08-17-2009, 10:48 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks