Home > other >  how to place an int into a 2D array (int[][]) in Java
how to place an int into a 2D array (int[][]) in Java

Time:12-08

I want make a method that places a number into one of the columns of a board. The column will be obtained from input from the user but i'll be using column "B" throughout this post.

The board looks like this:

1 2 3 2 2 3
3 2 2 1 1 2
3 1 1 1 2 2
2 2 1 3 2 2
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0

If the user chooses to "shoot" column "B" the method should convert the original board into this new board

1 2 3 2 2 3
3 2 2 1 1 2
3 1 1 1 2 2
2 2 1 3 2 2
0 1 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0

I'm having problems on how to replace the zero in column B that is just below the numbers with number 1. What I had planned for the method was to make several "if" statements for each of the columns and then use for-loops to scan throughout the array until it finds the zero on the column chosen that is just below the numbers.

this is what I have so far.

public class putNumberSO {
    public static int[][]board = { {1, 2, 3, 2, 2, 3}, 
                                   {3, 2, 2, 1, 1, 2}, 
                                   {3, 1, 1, 1, 2, 2}, 
                                   {2, 2, 1, 3, 2, 2}, 
                                   {0, 1, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0} };
    
    public static void main(String[] args) {
        printGrid(putNumber(board, "B"));
    }
    
    public static int[][] putNumber(int[][] board, String columnInput) {
        if(columnInput.equals("A") ) {
            for(int row = 0; row < board.length; row  ) {
                board[row][1] = 1;
            }
        }
        if(columnInput.equals("B") ) {
            for(int row = 0; row < board.length; row  ) {
                board[row][2] = 1;
            }        
        }
        if(columnInput.equals("C") ) {
            for(int row = 0; row < board.length; row  ) {
                board[row][3] = 1;
            }     
        }
        if(columnInput.equals("D") ) {
            for(int row = 0; row < board.length; row  ) {
                board[row][4] = 1;
            }    
        }
        if(columnInput.equals("E") ) {
            for(int row = 0; row < board.length; row  ) {
                board[row][5] = 1;
            }       
        }
        if(columnInput.equals("F") ) {
            for(int row = 0; row < board.length; row  ) {
                board[row][6] = 1;
            }        
        }
        return board;
    }
    
    public static void printGrid(int[][] grid) {
        for(int i = 0; i < grid.length; i    ) {
            for(int j = 0; j < grid[i].length; j    ) {
                System.out.print(grid[i][j]);
            }
            System.out.println();
        }
    }
}

The result from this code converts every number in column in "B" and is the part where I am confused.

Output from this code:

1 2 3 1 2 3
3 2 2 1 1 2
3 1 1 1 2 2
2 2 1 1 2 2
0 0 0 1 0 0 
0 0 0 1 0 0 
0 0 0 1 0 0

CodePudding user response:

I'm suggesting 2 solutions here.

  1. Just a simple fix for your problem Change the number only if it is 0
public class putNumberSO {
    public static int[][]board = { {1, 2, 3, 2, 2, 3}, 
                                   {3, 2, 2, 1, 1, 2}, 
                                   {3, 1, 1, 1, 2, 2}, 
                                   {2, 2, 1, 3, 2, 2}, 
                                   {0, 1, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0} };
    
    public static void main(String[] args) {
        printGrid(putNumber(board, "B"));
    }
    
    public static int[][] putNumber(int[][] board, String columnInput) {
        if(columnInput.equals("A") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][1] == 0) {
                    board[row][1] = 1;
                    break;
                }
            }
        }
        if(columnInput.equals("B") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][2] == 0) {
                    board[row][2] = 1;
                    break;
                }
            }        
        }
        if(columnInput.equals("C") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][3] == 0) {
                    board[row][3] = 1;
                    break;
                }
            }     
        }
        if(columnInput.equals("D") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][4] == 0) {
                    board[row][4] = 1;
                    break;
                }
            }    
        }
        if(columnInput.equals("E") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][5] == 0) {
                    board[row][5] = 1;
                    break;
                }
            }       
        }
        if(columnInput.equals("F") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][6] == 0) {
                    board[row][6] = 1;
                    break;
                }
            }        
        }
        return board;
    }
    
    public static void printGrid(int[][] grid) {
        for(int i = 0; i < grid.length; i    ) {
            for(int j = 0; j < grid[i].length; j    ) {
                System.out.print(grid[i][j]);
            }
            System.out.println();
        }
    }
}
  1. A better way to handle your code
public class putNumberSO {
    public static int[][]board = { {1, 2, 3, 2, 2, 3}, 
                                   {3, 2, 2, 1, 1, 2}, 
                                   {3, 1, 1, 1, 2, 2}, 
                                   {2, 2, 1, 3, 2, 2}, 
                                   {0, 1, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0}, 
                                   {0, 0, 0, 0, 0, 0} };
    
    public static void main(String[] args) {
        printGrid(putNumber(board, "B"));
    }
    
    public static int[][] putNumber(int[][] board, String columnInput) {
        int columnIndex = 0
        switch(columnInput) {
          case "A": columnIndex = 0;
                    break;
          case "B": columnIndex = 1;
                    break;
          case "C": columnIndex = 2;
                    break;
          case "D": columnIndex = 3;
                    break;
          case "E": columnIndex = 4;
                    break;
          case "F": columnIndex = 5;
                    break;
        }
        for(int row = 0; row < board.length; row  ) {
            if(board[row][columnIndex] == 0) {
                board[row][columnIndex] = 1;
                break;
            }
        }
        return board;
    }
    
    public static void printGrid(int[][] grid) {
        for(int i = 0; i < grid.length; i    ) {
            for(int j = 0; j < grid[i].length; j    ) {
                System.out.print(grid[i][j]);
            }
            System.out.println();
        }
    }
}

CodePudding user response:

You should check if the cell is 0 or not, then assign it with number 1, and break the loop.

try this

public static int[][] putNumber(int[][] board, String columnInput) {
        if(columnInput.equals("A") ) {
            for(int row = 0; row < board.length; row  ) {
                if (board[row][1] ==0){
                   board[row][1] = 1;
                   break;
                }
            }
        }
        else if(columnInput.equals("B") ) {
            for(int row = 0; row < board.length; row  ) {
                if (board[row][2] ==0){
                   board[row][2] = 1;
                   break;
                }
            }        
        }
        else if(columnInput.equals("C") ) {
            for(int row = 0; row < board.length; row  ) {
                if (board[row][3] ==0){
                   board[row][23] = 1;
                   break;
                }
            }     
        }
        else if(columnInput.equals("D") ) {
            for(int row = 0; row < board.length; row  ) {
                if (board[row][4] ==0){
                   board[row][4] = 1;
                   break;
                }
            }    
        }
        else if(columnInput.equals("E") ) {
            for(int row = 0; row < board.length; row  ) {
               if (board[row][5] ==0){
                   board[row][5] = 1;
                   break;
                }
            }       
        }
        else if(columnInput.equals("F") ) {
            for(int row = 0; row < board.length; row  ) {
                if (board[row][6] ==0){
                   board[row][6] = 1;
                   break;
                }
            }        
        }
        return board;
    }
    ```

CodePudding user response:

if(columnInput.equals("A") ) {
    for(int row = 0; row < board.length; row  ) {
        if(board**[row][0]** == 0){
            board**[row][0]** = 1;
            break;
        }
    }
}

Firstly, to check for "A", you have to use board[row][0]. You can use a "if" condition and after the condition is true, you can exit from for loop.

CodePudding user response:

First, when I ran your code, I didn't get the output that matches your question. The actual output I got is this:

121223
321112
311122
221322
011000
001000
001000
001000
001000
001000

A small point: To make it easier to read, you can add spaces between columns: System.out.print(grid[i][j] " ");

Also, your code made the 3rd column all 1, but your question has it the 4th column.

Now, let's talk about the bugs:

Your example indicates you wanted "B" to be associated with the 2nd column. This seems to indicate a small amount of confusion about Java indices. Java indices are zero-based. Your code associates "A" with column 1, "B" with column 2, and so on. You seem to have wanted "A" to indicate column 0, "B" to indicate column 1, and so on.

Let me interrupt the discussion about bugs with a way to condense your code:

It isn't necessary to use those several if statements to check the column. Here is some information you can take advantage of:

  • Java uses Unicode for characters
  • In Unicode, the letters of the alphabet 'A' to 'Z' are consecutive.[1]
  • In Java, characters are an integer type.

So, in Java, characters can be treated as numbers. You can do arithmetic on them. The character 'A' has a decimal value of 65. The character 'B' is 66, and so on. [2]

So, you can eliminate the bunch of if statements, and use one for loop by using this statement:

 int column = columnInput - 'A';

However, I changed columnInput from a String to a char. The corresponding call gets changed to this:

putNumber(board, 'B'));

That's because, in Java, a character between single quote marks represents a char literal. When between double quote marks, as in "B", it's a character String.

If you need to leave it a character String, use a line like this:

 int column = columnInput.charAt(0) - 'A';

Back to the bugs:

Next, your code isn't actually looking for the first zero. Your code loops through each row, changing whatever is in the designated column to 1.

for(int row = 0; row < board.length; row  ) {
                board[row][1] = 1;
            }

Here is where you need to have an if statement. Inside the for loop, you want to check if you have found a 0.

Once you find a 0 and change it to a 1, you want to exit the for loop. That is done with the break statement.

  public static int[][] putNumber (int[][] board, char columnInput) {
          int column = columnInput - 'A';
          for (int row = 0; row < board.length;   row) { 
              if (board [row][column] == 0) { 
                  board [row][column] = 1;
                  break;
              }
          }
          return board;
      }

Notes [1]:

  • The same is true for the letters 'a' to 'z'.
  • This is also true for ASCII
  • EBCDIC, on the other hand, has non-letter characters between 'I' and 'J', and between 'R' and 'S'.

Note [2]: Be careful. The character '1' isn't the same as the integer '1'. The character '1' is represented with the integer value 61.

  • Related