0

I have a 2D Array and need to make a for loop that goes through each row and finds the index+1 when the integers stop increasing consecutively. For example if the first row is {1,2,3,4,9,10,11,20}, my method should set count1 = 4. The break statement is meant to terminate the inner loop and move on to the next sequence of the outer loop.

 public static int[][] reshuffle(int[][] board) {
        int count1 =0;
        int count2 =0;
        int count3 =0;
        int count4 =0;
        for(int i=0;i<4;i++) {
            for (int j = 0; j < 14; j++) {
                if (i==0 && board[i][j] + 1 != board[0][j + 1]) {
                    count1 = j+1;
                    break;
                } else if (i==1 && board[i][j] + 1 != board[1][j] + 1) {
                    count2 = j+1;
                    break;
                } else if (i==2 && board[i][j] + 1 != board[2][j] + 1) {
                    count3 = j+1;
                    break;
                } else if (i==3 && board[i][j] + 1 != board[3][j] + 1) {
                    count4 = j+1;
                    break;
                }

            }
        }
}

My program will return the correct value for count1, but always returns 0 for count2, count3, and count4. This indicates to me that the break statement is somehow terminating the outer loop as well as the inner.

tdammon
  • 599
  • 1
  • 7
  • 26
  • `break` only exits the inner loop - it looks like now would be a good time to learn how to use a debugger. – assylias Oct 11 '17 at 14:59
  • Can you provide a `int[][] board` example ? the `break` statement may takes you out from the double `for loop`. You'll need to consider another way. For example, you could try putting the second `for loop` in a `public int getCountForRow(int[] row)` method – Al-un Oct 11 '17 at 15:01

2 Answers2

3

I think you have a logic error, given that i = 3 board[i][j] + 1 will be equal to board[3][j] + 1 I think what you meant to do was this:

public static int[][] reshuffle(int[][] board) {
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;
    int count4 = 0;

    for(int i=0;i<4;i++) {
       for (int j = 0; j < 14; j++) {
           if (i==0 && board[i][j] + 1 != board[0][j + 1]) {
               count1 = j+1;
               break;
           } else if (i==1 && board[i][j] + 1 != board[1][j + 1]) {
               count2 = j+1;
               break;
            } else if (i==2 && board[i][j] + 1 != board[2][j + 1]) {
                count3 = j+1;
                break;
             } else if (i==3 && board[i][j] + 1 != board[3][j + 1]) {
                count4 = j+1;
                break;
             }
        }   
    }
}
J0sh0nat0r
  • 211
  • 2
  • 8
0

You could use labels and break to those labels, but this is not a good approach:

public static int[][] reshuffle(int[][] board) {
        int count1 =0;
        int count2 =0;
        int count3 =0;
        int count4 =0;
        for(int i=0;i<4;i++) {
            level1:
            for (int j = 0; j < 14; j++) {
                if (i==0 && board[i][j] + 1 != board[0][j + 1]) {
                    count1 = j+1;
                    break level1;
                } else if (i==1 && board[i][j] + 1 != board[1][j] + 1) {
                    count2 = j+1;
                    break level1;
                } else if (i==2 && board[i][j] + 1 != board[2][j] + 1) {
                    count3 = j+1;
                    break level1;
                } else if (i==3 && board[i][j] + 1 != board[3][j] + 1) {
                    count4 = j+1;
                    break level1;
                }

            }
        }
}

I would suggest refactoring the code to avoid inner loops, e.g. use a separate method.

schneida
  • 729
  • 3
  • 11
  • 37