Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java unreachable statement

Tags:

java

arrays

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?

like image 241
test Avatar asked Jan 25 '26 21:01

test


2 Answers

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.

like image 182
Matt Mitchell Avatar answered Jan 27 '26 11:01

Matt Mitchell


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'.

like image 40
Lyle Avatar answered Jan 27 '26 11:01

Lyle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!