Reputation: 1038
I have a section of code that is puzzling me. I define an integer array inside an if/else statement because the length of the array depends on the length of 2 inputs to the method. My problem is that outside the if/else statement, the variable definition 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 get is on the statement String result = Arrays.toString(output);
where Eclipse tells me that output
cannot be resolved to a variable.
...and by the way, yes, I know that this is not the way to add two integer arrays -- I reduced this from more complex code to demonstrate the problem!
Upvotes: 3
Views: 8482
Reputation: 213233
Well, you have already got the solution, but I would like to point out that, you can reduce your methods, to avoid duplicating codes, which you are currently doing.
You can make use of conditional operators
to create the array according to the result of L1 > L2
. And rather than iterating till L1
or L2
, you should iterate till the length of array output
.
So, you can try using the 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 Java Naming Conventions. Method name should start with lowercase alphabets.
Upvotes: 2
Reputation: 10151
Scope of a variable is always the next enclosing {
}
.
Of cause starting at its declaration (not at the {
)
Upvotes: 3
Reputation: 427
You are declaring the output variable as a local variable inside each of the if/else statements. To fix this, declare it first outside then adjust it and return the results. This keeps it in the scope 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;
}
Upvotes: 0
Reputation: 9206
Define output
before if
statement. 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 the if
statement it simply had only that block scope.
Upvotes: 5