Jared Waxler Jared Waxler - 1 month ago 10
Java Question

Elements of 2d interger arrays not getting compared. not trying to compare the whole array

I'm just brushing up on my java skills because its been a while since I have coded. I have looked at many posts of my question and found that I seem to be comparing everything correctly as far as I can tell. I am comparing two 2d array elements against each other and if that match I replace the character at the element, However it seems to just go out of bounds when trying to compare them. Not seeing the out of bounds error (line 48).



char[][] board = new char[3][3];
char[][] player1 = new char[1][1];
char[][] player2 = new char[1][1];
int playerRow = 0;
int playerCol = 0;
Scanner kbd = new Scanner(System.in);

System.out.println("Lets play a simple game of tic-tac-toe");
System.out.println("Player 1 (X's) : Please enter a row number and column number ");
System.out.println(" in order to plot the cordinates of your desired move");
playerRow = kbd.next().charAt(0);
playerCol = kbd.next().charAt(0);
for(int row = 0; row < board.length; row++)
{
for(int col = 0; col < board[row].length;col++)
{
if (board[row][col] == player1[playerRow][playerCol])
{
board[row][col] = 'X';
System.out.print(board[row][col]+" ");
}
else
{
board[row][col]= '-';
System.out.print(board[row][col]+" ");

}
}
System.out.println();
}




Answer

Your code seems like you're taking the wrong approach to solving this. Unless I'm misunderstanding your purpose, I don't think player1 or player2 are necessary. Everything should be stored just in board. Here's an example of what that might look like:

//initialize variables
char[][] board = new char[3][3];
int playerRow = 0;
int playerCol = 0;

//clear the board
for(int row = 0; row < board.length; row++){
    for (int col = 0; col < board[row].length; col++){
        board[row][col] = '-';
    }
}

Scanner kbd = new Scanner(System.in);

System.out.println("Lets play a simple game of tic-tac-toe");
System.out.println("Player 1 (X's) : Please enter a row number and column number ");
System.out.println(" in order to plot the cordinates of your desired move");

//get player's row and column
playerRow = kbd.nextInt();
playerCol = kbd.nextInt();

//Change the chosen spot to be the player's character.
board[playerRow][playerCol] = 'X';

//Display the board
for(int row = 0; row < board.length; row++){
    for(int col = 0; col < board[row].length;col++){
        System.out.print(board[row][col] + " ");
    }
    System.out.println();
}

That's an example of one move, which lets the player pick a spot, and then displays the board.

The reason for the error you're currently getting is because you read in the character '0', and then try to use that as an index into the array. But '0' is not the same as 0; it's actually the unicode value representing the character 0, which happens to have the value 48, which is not a valid index into your array. You should take that input as an integer, and then simply set that value in the array (without the loops to find the correct spot).