-1

Im writing a method to factorize a number and for some reason when I try to print factorA it says it isn't recognized, any suggestions?

    public static void factorize(int n){
    if (n % 2 == 0){                                                //If number is even
        int factorA = 2;
        int factorB = n/2;
    }else{                                                          //If number is odd
        int halfLine = n/2;                                         //Only variables that are even will be factors greater than half of them, this case is only for odd integers
        int smallestFactorOdd = 3;

        while (smallestFactorOdd < halfLine){                       //Until you've found the smallest factor for add variables
            if (n % smallestFactorOdd == 0){                        //If smallestFactorOdd is a factor of n
                int factorA = smallestFactorOdd;
                int factorB = n/smallestFactorOdd;
            }else{
                smallestFactorOdd += 2;
            }
        }
    }
    System.out.println(factorA);
}

Updated Code(Still Receiving Error):

public static void factorize(int n){
    int factorA;
    int factorB;

    if (n % 2 == 0){                                                //If number is even
        factorA = 2;
        factorB = n/2;
    }else{                                                          //If number is odd
        int halfLine = n/2;                                         //Only variables that are even will be factors greater than half of them, this case is only for odd integers
        int smallestFactorOdd = 3;

        while (smallestFactorOdd < halfLine){                       //Until you've found the smallest factor for add variables
            if (n % smallestFactorOdd == 0){                        //If smallestFactorOdd is a factor of n
                factorA = smallestFactorOdd;
                factorB = n/smallestFactorOdd;
            }else{
                smallestFactorOdd += 2;
            }
        }
    }
    System.out.println(factorA);
    System.out.println(factorB);
}

3 Answers3

2

You declare factorA inside your while loop. Any variable declared inside a loop does not exist outside of the loop. (Other classes and methods aren't even aware that the variable exists) You need to declare the variable outside of the loop

Like this:

public static void factorize(int n){
    int factorA = 0;
    int factorB = 0;
    ...

EDIT:

You have to set the variable to zero when you declare it. Otherwise there is a chance that it will never be given a value. Since it is a method local variable it must be initialized before it is used. (You then try to print it. The compiler doesn't know the value, so it complains)

Further reading about second error:

Default Values and Initialization in Java

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
0

You have to initialize the local variables before they are used.

you don't need to initialize primitives only in case of instance variables.

Instance variables gets initialized at the time of creation of object of that class. But variables defined in method's scope has to be initialized.

So, inside your method, assign 0 to your local variables and it would work.

public static void factorize(int n){
int factorA = 0;
int factorB = 0;

if (n % 2 == 0){                                                //If number is even
    factorA = 2;
    factorB = n/2;
}else{                                                          //If number is odd
    int halfLine = n/2;                                         //Only variables that are even will be factors greater than half of them, this case is only for odd integers
    int smallestFactorOdd = 3;

    while (smallestFactorOdd < halfLine){                       //Until you've found the smallest factor for add variables
        if (n % smallestFactorOdd == 0){                        //If smallestFactorOdd is a factor of n
            factorA = smallestFactorOdd;
            factorB = n/smallestFactorOdd;
        }else{
            smallestFactorOdd += 2;
        }
     }
  }
  System.out.println(factorA);
  System.out.println(factorB);
}
miiiii
  • 1,580
  • 1
  • 16
  • 29
0

Both declaration and initialisation must be outside the if

public static void factorize(int n){
    int factorA = 0;
    int factorB = 0;

    if (n % 2 == 0){                                                 
        factorA = 2;
        factorB = n/2;
    }else{                                                           
        int halfLine = n/2;                                         
        int smallestFactorOdd = 3;
tzuxi
  • 117
  • 1
  • 9