Cannot find symbol?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sam S
    New Member
    • Apr 2011
    • 1

    Cannot find symbol?

    I know this should be a simple fix but I can't figure it out. This is my class that generates the factors for a given number. It works until I try to use the return, in which it says cannot find symbol: variable a, even though it has been declared. I bolded the line the error is on. Any help?

    Code:
    import java.util.Scanner;
    public class FactorGenerator 
    {       
                    public static int initialNumber;
             
                 public FactorGenerator()
                 {
                     for (int a=1; a < initialNumber; a++)
                     {   
                          if(initialNumber%a == 0)
                          {
                           
                            System.out.println(a + " ");
                          }
                     }
                 }           public static int getFactors()
                             [B]{
                             return (a +"");
                             }[/B]                [CODE]
    }[/CODE]
    Last edited by Meetee; Apr 4 '11, 06:24 AM. Reason: Use code tags
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    variable a is inside the scope of for loop. So it will not be available out side the loop. As you can see you are trying to access the variable in a getter method.

    You can use the class variable for this purpose or any other means of data retrieval. But obviously you can not use variable which is not in scope.

    Check this link.

    Regards
    Dheeraj Joshi
    Last edited by Dheeraj Joshi; Apr 7 '11, 06:23 AM. Reason: Added link.

    Comment

    Working...