Create columns with arrays, wrong output

So, I've been trying to make some simple columns in Java (yes, I'm very familiar with this) and I just can't get it to "stand" correctly ...

My code is as follows:

int[][] grid = {
            {3, 5, 2434},
            {2, 4},
            {1, 2, 3, 4}
        };

      

in conjunction 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

This leads me to my confusion. Shoudn't "\ t" just move them like "tab" instead of giving them a newline?

I appreciate the answers.

+3


source to share


6 answers


Use System.out.print

instead System.out.println

. println

automatically adds a line and makes 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();
}

      

+1


source


System.out.println(grid[row][col] + "\t");

      

You are using println

that automatically adds the line at the end String

that you pass.



Try using instead System.out.print()

.

+1


source


you're using

System.out.println() ;

      

this method by default appends a newline to the end of the output.

if you don't want this behavior to use

System.out.print();

+1


source


The next line is method related println()

and not because of \t

use print()

instead

Your method behaves like it calls print(yourParameters)

and then println () which says

Ends the current line by writing a line separator string . the line separator string is defined by the system property line.separator and is not necessarily a single newline character ('\ n').

Source

+1


source


Use System.out.print () instead of System.out.prinln () to avoid line feed

0


source


Use System.out.print (). the println command creates a new line at the end of each command, as if you had written System.out.print (text + "\ n").

0


source







All Articles