Java variable defined inside a loop doesn't seem to be recognized outside of the loop?

I have a section of code that puzzles me. I am defining an integer array inside an if / else statement because the length of the array depends on the length of the two method inputs. My problem is that outside of the if / else statement the definition of the variable seems to be lost.

import java.util.Arrays;

public class test {

  public String AddArrays(int [] arg1, int [] arg2) {
    int L1 = arg1.length;
    int L2 = arg2.length;
    if (L1 > L2) {
        int[] output = new int[L2];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg2[i];
        }
    } else {
        int[] output = new int[L1];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg1[i];
        }
    }
    String result = Arrays.toString(output);
    return result;
    }
}

      

The error I am getting is for an expression String result = Arrays.toString(output);

where Eclipse tells me it output

cannot be resolved to a variable.

... and by the way, yes, I know this is not a way to add two integer arrays - I minified that from more complex code to demonstrate the problem!

+3


source to share


4 answers


Define output

before expression if

. Like this:

int[] output;
int L1 = arg1.length;
int L2 = arg2.length;
if (L1 > L2) {
    output = new int[L2];
    for (int i = 0; i < L2; i++) {
        output[i] = arg1[i] + arg2[i];
    }
} else {
    output = new int[L1];
    for (int i = 0; i < L2; i++) {
        output[i] = arg1[i] + arg1[i];
    }
}
String result = Arrays.toString(output);
return result;
}

      



When you declared output

inside a statement if

, it just had only that block area.

+5


source


Volume is always a variable as follows: {

}

.



Reason starting with her announcement (not on {

)

+3


source


Well, you already have a solution, but I would like to point out that you can reduce your methods to avoid duplicating the codes you are currently doing.

You can use conditional operators

to create an array according to the result L1 > L2

. And instead of iterating before L1

or L2

, you should iterate over the length of the array output

.

So, you can try using below code: -

public String addArrays(int [] arg1, int [] arg2) {
    int L1 = arg1.length;
    int L2 = arg2.length;

    int[] output = L1 > L2 ? new int[L2]: new int[L1];

    for (int i = 0; i < output.length; i++) {
        output[i] = arg1[i] + arg2[i];
    }

    return Arrays.toString(output);
}

      

And please follow the Java Naming conventions. Method names must begin with lowercase alphabets.

+2


source


You declare the output variable as a local variable inside each of the if / else statements. To fix this, first declare it outside, then customize it and return the results. This keeps it in between the brackets { }

.

public String AddArrays(int [] arg1, int [] arg2) {
    int L1 = arg1.length;
    int L2 = arg2.length;
    int[] output;
    if (L1 > L2) {
        output = new int[L2];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg2[i];
        }
    } else {
        output = new int[L1];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg1[i];
        }
    }
    String result = Arrays.toString(output);
    return result;
}

      

0


source







All Articles