Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output in a table format in Java's System.out

I'm getting results from a database and want to output the data as a table in Java's standard output

I've tried using \t but the first column I want is very variable in length.

Is there a way to display this in a nice table like output?

like image 861
Chris Avatar asked Apr 30 '10 14:04

Chris


People also ask

What is system out format in Java?

Java Programming Java8Object Oriented Programming. String provides format() method can be used to print formatted output in java. System. out. printf() method can be used to print formatted output in java.


1 Answers

Use System.out.format . You can set lengths of fields like this:

System.out.format("%32s%10d%16s", string1, int1, string2); 

This pads string1, int1, and string2 to 32, 10, and 16 characters, respectively.

See the Javadocs for java.util.Formatter for more information on the syntax (System.out.format uses a Formatter internally).

like image 157
Michael Myers Avatar answered Oct 13 '22 00:10

Michael Myers