0

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

talex
  • 17,973
  • 3
  • 29
  • 66
DevanDev
  • 245
  • 2
  • 11
  • 5
    your `return rows` statement returns `rows` and ends the method (going back to the caller, with the provided value). If you want to return multiple values, you need to return an object, like an `int[]` or even better - a [`Pair`](http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/tuple/Pair.html) – amit Sep 18 '14 at 09:15
  • what do you think `return rows; return cols;` mean? – talex Sep 18 '14 at 09:17
  • 1
    Looks like you also want `rows=i` and `cols=j` rather than `i=rows` and `j=cols`. At the moment, `rows` and `cols` are both zero at the end, because you never write to them. – chiastic-security Sep 18 '14 at 09:30

3 Answers3

3

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.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

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.
    }
kirti
  • 4,499
  • 4
  • 31
  • 60
1

You have already breaked from the code using return rows;. This statement returns to the caller. So, a statement after return rows; is inaccessible

Deval Khandelwal
  • 3,458
  • 1
  • 27
  • 38