Reputation: 41
So I was trying to make some simple columns in Java (Yes, I am very new to this), and I just can't get it to 'stand' properly...
My code is the following:
int[][] grid = {
{3, 5, 2434},
{2, 4},
{1, 2, 3, 4}
};
combined with:
for(int row=0; row<grid.length; row++){
for (int col=0; col < grid[row].length; col++) {
System.out.println(grid[row][col] + "\t");
}
System.out.println();
}
Which gives me this result:
3
5
2434
2
4
1
2
3
4
Which leads me to my confusion.. Shoudn't "\t" just move them like "tab", instead of giving them a new line?
I appreciate the answers.
Upvotes: 3
Views: 43
Reputation: 93
Use System.out.print(). the println command creates a new line at the end of every command , as if you wrote System.out.print(text+"\n").
Upvotes: 0
Reputation: 7919
The next line is because of the method println()
and not because of \t
use print()
instead
Your method behaves as though it invokes print(yourParameters)
and then println() which says
Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').
Upvotes: 1
Reputation: 1679
System.out.println(grid[row][col] + "\t");
You are using println
which automatically adds a linefeed at the end of the String
you pass it.
Try using System.out.print()
instead.
Upvotes: 1
Reputation: 364
Use System.out.print() instead of System.out.prinln() in order to avoid line feed
Upvotes: 0
Reputation: 3963
Use System.out.print
instead of System.out.println
. println
automatically adds a linebreak and renders your tab useless.
for(int row=0; row<grid.length; row++){
for (int col=0; col < grid[row].length; col++) {
System.out.print(grid[row][col] + "\t");
}
System.out.println();
}
Upvotes: 1
Reputation: 5423
you are using
System.out.println() ;
this method by default adds a new line at the end of the output.
if you don't want this behaviour use
System.out.print();
Upvotes: 1