0

I have an x by y board of numbers stored in a two dimensional array in java that I would like to print out to the user in a nice formatted manner. Would there be an easy way to do this in java?

Here is how I build my board:

board = new int[TotRow][TotCol];
1
  • 1
    Please put in some code that explains your case Commented May 26, 2015 at 3:46

6 Answers 6

2

You might prefer the one line, because it's easy, Arrays.deepToString(Object[]) the Javadoc says (in part),

This method is designed for converting multidimensional arrays to strings.

System.out.println(Arrays.deepToString(board));
Sign up to request clarification or add additional context in comments.

Comments

1

If you want the number to be padded properly as well use String.format(),

for (int i = 0; i < TotRow; i++) {
    for (int j = 0; j < TotCol; j++) {
        System.out.print(String.format("%3d", board[i][j]) + "\t");
    }
    System.out.println();
}

Notice that I have used %3d for formatting, you can use format as required by you

Comments

0

You can print the board two dimensional array like this

for (int i = 0; i < board.length; i++) {
    for (int j = 0; j < board[i].length; j++) {
        System.out.print(board[i][j] + "\t");
    }
    System.out.println();
}

Comments

0

Since I don't like relying on tabs, I'd like to mention some approaches that don't rely on them.

If you know that all of the numbers in the table are nonnegative numbers of, say, 4 digits or less, you can use String.format in a manner similar to Nitin's answer:

for (int i = 0; i < TotRow; i++) {
    for (int j = 0; j < TotCol; j++) {
        System.out.print(String.format("%4d ", board[i][j]));
    }
    System.out.println();
}

The %4d means to format the number using a width of 4 characters, and pad with blanks on the left if the number takes fewer than 4 characters to print. Thus, if there aren't any 5-digit or longer numbers, each print will print exactly 5 characters--a 4-character field for the number, and a space after the number. This will make everything line up, as long as none of the numbers is too large. If you do get a number that takes more than 4 characters, the formatting will get messed up.

Here's a way to adjust the column width so that it's just large enough to hold all the values in the table, and works fine if there are negative numbers:

int maxWidth = 0;
for (int i = 0; i < TotRow; i++) {
    for (int j = 0; j < TotCol; j++) {
        maxWidth = Math.max(maxWidth, Integer.toString(board[i][j]).length());
    }
}
String format = "%" + maxWidth + "d ";
for (int i = 0; i < TotRow; i++) {
    for (int j = 0; j < TotCol; j++) {
        System.out.print(String.format(format, board[i][j]));
    }
    System.out.println();
}

Note: not tested

Comments

0
for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
        System.out.print(board[i][j] + "\t");
    }
    System.out.println();
}

4 Comments

inner loop System.out.print instead
This may work (after changing the first println to print), but I tend to avoid using tabs because you really don't have any control over how they work or whether they do anything at all.
can you add some explanation?
Note also that there's a big difference between this answer and Nitin's: using String.format("%3d") causes the numbers to be right-justified in each column (i.e. the low-order digits line up), while this left-justifies them (the high-order digits line up). Which one the OP wants is a matter of preference, but most people are used to seeing tables of integers right-justified, I think.
0

You basically just use a nested loop.

for (int i = 0; i < board.length; i++) {
    for (int j = 0; j < board[i].length; j++) {
        System.out.print(board[i][j] + " ");
    }
    System.out.println();
}

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.