I have a piece of code
for(int i = 0; i < num_of_random; i++){
String str = in.readLine();
if(str != null){
String[] randoms = new String[4];
randoms = str.split(",");
dateRanges[i] = Integer.parseInt(randoms[0]);
id[i] = Integer.parseInt(randoms[1]);
flag[i] = Integer.parseInt(randoms[2]);
system[i] = Integer.parseInt(randoms[3]);
}
}
When I run this code against findBugs, I get a suggestion for
“String[] randoms = new String[4];”
This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often, this indicates an error, because the value computed is never used.
Why do I get this?
Thanks a lot
Because you initialize a variable to a value (
new String[4]), and then replace the variable value with another one (the result ofstr.split(",")) just after. The initialization is thus not necessary.Your code is functionally equivalent to
except it allocates a new String array which is immediately discarded.