Reputation: 11
so this is what my code actually looks like, i was trying to make a simple 2d 10 by 10 array with just zeros at first. The zeros print in one column with breaks where a new column should be starting
int[][] board = new int[10][10];
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
board[x][y]=0;
}
}
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
System.out.println(board[x][y] +"");
}
System.out.println("");
}
it prints like this...
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
etc. I've tried other formats, they usually come out the same.
I want it to look something like...
0000000000
0000000000
0000000000
etc.
Upvotes: 1
Views: 85
Reputation: 4738
In your code you used println
which will give output per line. Use print
instead. Also I have removed +""
for(int i=0;i<10;i++){
for(int j=0; j<10;j++){
System.out.print(board[i][j]);
}
System.out.println("");
}
Upvotes: 0
Reputation: 309
Using println, notice the ln suffix, indicates it adds a \n (new line) or \r (carriage return) or something similar causing a new line.
You will need to use a similar but seperate print function to print without the new line, like print.
So replace
System.out.println(board[x][y] +"");
with
System.out.print(board[x][y] +"");
This will print the positions in rows. But you will need to add a carriage return between each row. Which you already did with this line:
System.out.println("");
Upvotes: 2
Reputation: 119
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
System.out.println(board[x][y] +"");
}
System.out.println("");
}
should be
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
System.out.print(board[x][y] +" ");
}
System.out.println("");
}
bye
Upvotes: 0
Reputation: 394
Why not use a StringBuilder and build line by line, then print.
for (int x = 0; x < 10; x++) {
StringBuilder sb = new StringBuilder();
for (int y = 0; y < 10; y++) {
sb.append(" " + board[x][y]);
}
System.out.println(sb.toString());
}
Upvotes: 0