tags:

views:

74

answers:

3

I'm trying to code a method which loads a map according from a number inside loadBoard(NUMBER_HERE); But I get 'unreachable statment' on the line

return board;

Here is my code:

public int[][] loadBoard(int map) {
    if (map == 1) {   return new int[][] { 

 {2,2,24,24,24,24,24,3,3,0,0,0,1 },

 { 2,2,24,23,23,23,24,1,3,0,0,0,1 },

 { 1,1,24,23,23,23,24,1,3,3,3,3,1 },

 { 1,1,24,24,23,24,24,1,1,1,1,3,1 },

 { 1,1,1,1,7,1,1,1,1,1,1,3,1 },

 { 5,1,1,1,7,7,7,7,7,1,1,1,1 },

 { 6,3,3,1,3,3,3,1,7,7,7,3,1 },

 { 6,3,3,1,3,1,1,1,1,1,7,1,1 },

 { 3,3,1,1,1,1,1,1,1,1,7,1,1 } };

 }else{

 return new int[][] {

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },


 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,7,1,1,24,24,24,24,1,1,1,1 },

 { 1,1,7,1,1,24,1,24,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,3,3,1,1,24,1,1,1,1,1,1,1 },

 }; } 

 return board;  }

What am I doing wrong?

+7  A: 

Your last line return board;

If you look at your code you have:

if (condition)
  return /* Some things go here */
else
  return /* The rest go here */
return /* But who goes here? */

The answer is to remove that Unreachable line of code as it is literally pointless.

Graphain
So.. what do I do? I am stuck; hence I asked for help. :(
Dan
I see my error, if that's what you're trying to get at. But I don't know how to fix it.
Dan
Change 'return new in[][]' to 'board = new int[][]' and then return board in the end
Samit G.
Just remove that line of code.
Graphain
+1  A: 

In each of the "if" and "else" blocks, you return a value. Since the execution path will always enter one of these blocks, you'll always return from one of them, and you'll never fall through to 'return board'.

Lyle
+1  A: 

Basically you have a pattern like:

if(condition)   
    return val_a;
else
    return val_b;

return val_c;

Since your 'else' is unconditional, either 'if' or 'else' is guaranteed to execute. So last return is unreachable.

Samit G.