Reputation: 355
for the following code mentioned below ,I have been obtaining the Error " Unreachable statement error " at " Return Cols " statement
The code computes the position of Maximum Dose in a generated Output CSV file
public int getPosition() {
double dose = 0.0;
double position = 0.0;
int rows = 0;
int cols = 0;
String s;
for (int j = 1; j < nz; j++) {
s = "";
for (int i = 1; i < nx; i++) {
for (DetEl det_el : det_els) {
if (det_els.get(j + i * nz).getDose() == getMaxDose()) {
i=rows;
j=cols;
}
// comma separated or Semicolon separated mentioned here
}
// prints out the stream of values in Doses table separated by Semicolon
}
}
return rows;
return cols;//unreachable statement error obtained at this position.
}
Any help is greatly appreciated
Upvotes: 0
Views: 141
Reputation: 35587
You can't do this.
return rows; // when your program reach to this your program will return
return cols; // then never comes to here
If you want to return multiple values from a method, you can use a Array
or your own Object
Eg:
public int[] getPosition(){
int[] arr=new int[2];
arr[0]=rows;
arr[1]=cols;
return arr;
}
You should read this.
Upvotes: 3
Reputation: 3548
You have already breaked from the code using return rows;
. This statement returns to the caller. So, a statement after return rows;
is inaccessible
Upvotes: 1
Reputation: 4609
After return the code is not proceeded further thats why it is giving unreachable code error there coz u are returning rows and code exits there and thus return coloumns will not reached
public int getPosition() {
double dose = 0.0;
double position = 0.0;
int rows = 0;
int cols = 0;
String s;
for (int j = 1; j < nz; j++) {
s = "";
for (int i = 1; i < nx; i++) {
for (DetEl det_el : det_els) {
if (det_els.get(j + i * nz).getDose() == getMaxDose()) {
i=rows;
j=cols;
}
// comma separated or Semicolon separated mentioned here
}
// prints out the stream of values in Doses table separated by Semicolon
}
}
return rows;// code ends here itself thats why return cols is unreachable
return cols;//unreachable statement error obtained at this position.
}
Upvotes: 1