Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By Fubarable

Thread: Cannot find symbol variable

  1. #1
    xalebo is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Exclamation 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default Re: Cannot find symbol variable

    The scope of chars is only inside your length() method. You can't access it outside of that method.
    xalebo likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    xalebo likes this.

  4. #4
    xalebo is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default 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

  1. Replies: 4
    Last Post: 12-25-2010, 02:17 PM
  2. Cannot find symbol variable Keyboard...
    By Alnegar in forum New To Java
    Replies: 6
    Last Post: 10-30-2010, 06:39 PM
  3. Cannot find symbol variable - Why? I can.. ^^
    By Mattedatten in forum New To Java
    Replies: 4
    Last Post: 03-08-2010, 08:07 PM
  4. Cannot find symbol variable pD! I cant fix it!!!
    By Addez in forum New To Java
    Replies: 2
    Last Post: 09-17-2009, 09:32 PM
  5. Cannot find symbol variable yourScore
    By Addez in forum New To Java
    Replies: 4
    Last Post: 08-17-2009, 11:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •