-1

I am writing a game code. The board of this game is 10x10 size. In the beginning, I wrote this board as a two-dimensional integer array. Now I have to replace some places on the board with letters. It will change with letters according to the coordinates entered by the player.

private static int[][] board = new int[10][10]; //board

StringBuilder sb = new StringBuilder();
    for (int[] array : board) {
        sb.append('\n').append(Arrays.toString(array));
    }
    String new = sb.toString(); //I tried to convert this way

if (new[x][y].equals("r")){
    new[x][y] = "d"; //I'm getting an error in this part.
}
Amit kumar
  • 1,196
  • 6
  • 13
  • 20
  • why not using String array from the beginning? So in the first line you will have private static String[][] board = new String[10][10]; Then you have to change other types of the method accordingly – AM13 May 31 '20 at 09:12
  • The 'new' String variable is not a 2 dimensional array. So we cant change the value just like changes integer 2d array using [x][y]. – Sreejith May 31 '20 at 09:17
  • 1
    Can’t use java keywords as variables, also why are you checking for alpha when you were storing numeric values in the array? – G V May 31 '20 at 09:34
  • @anon Try using the 2D String array to copy the 2D int array board data. As shown in my answer. – Sreejith May 31 '20 at 09:59

3 Answers3

0

I would recommend you not use int as the array data type. Perhaps use a String, and you can use Integer.parseInt(x) to extract an integer from an array entry. If your numbers are single digits, using char would be a better choice.

If you want to do it that way, you'll have to do a lot of String operations and you'll have to store your entire board using a String. You might want to read this post to help you index a String like a 2D array.

Richard Yan
  • 1,206
  • 8
  • 19
0

Try using 2D String array, by copying data from the 2D int array.

int[][] board = new int[10][10]; //board

//Converting int array to String array
String[][] s = new String[board.length][];
for(int i = 0; i < board.length; i++){
    s[i] = new String[board[i].length];
    for(int j=0; j<board[i].length; j++){
        s[i][j] = Integer.toString(board[i][j]); 
    }
}

if (s[x][y].equals("r")){
    s[x][y] = "d"; 
}
Sreejith
  • 286
  • 2
  • 6
0

There are two major problems in your code:

  1. You have used new as the name of the variable which is not allowed as new is a reserved word, used for instantiating.
  2. The following statement does not produce a 2-D array of strings:

    String s = sb.toString();
    

    Therefore, your attempt to access/set s[x][y] will result in compilation error.

Recommendations:

  1. Use a String[][] instead of int[][]. A String[][] will be able to store strings consisting of letter(s) as well as integers represented as a string (which you can always parse to the corresponding integer by using Integer::parseInt if the need be).
  2. Create another array of type, String[][] and copy the elements of your int[][] array to it.

    I would go with the former one.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72