0

Im trying to print this 2d array

public class TicTacToe {

    public static void main(String[] args) {

        char[][] gameBoard = {
                {' ' + '|' + ' ' + '|' + ' '},
                {'-' + '+' + ' ' + '+' + '-'},
                {' ' + '|' + ' ' + '|' + ' '},
                {'-' + '+' + ' ' + '+' + '-'},
                {' ' + '|' + ' ' + '|' + ' '}
        };
        for(char[] row : gameBoard){
            for(char symbol : row){
                System.out.print(symbol);
            }
            System.out.println();
        }
     }
   }

however my output is just

Ř Ð Ř Ð Ř

What am i doing wrong? I used a youtube video to help and they did practically the same thing and it worked fine for them.

1
  • 3
    How would you initialize an int array? Would you write new int[]{1 + 2 + 3} or new int[]{1, 2, 3}? If it is the latter, why do you use a different approach in your code? Commented Apr 14, 2021 at 2:24

2 Answers 2

1

Use commas to separate the items in the array instead of adding the chars.

char[][] gameBoard = {
        {' ', '|', ' ', '|', ' '},
        {'-', '+', ' ', '+', '-'},
        {' ', '|', ' ', '|', ' '},
        {' ', '|', ' ', '|', ' '},
        {' ', '|', ' ', '|', ' '}
};
Sign up to request clarification or add additional context in comments.

Comments

1

Addition of two chars in java like ('a' + 'b') will result 195 which is sum of ascii values for a(97) and b(98). In your case the results are (344, 208,344,208,344). You can lookup the value here: https://asecuritysite.com/coding/asc2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.