Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I line up multiple values from System.out.print into columns neatly? [duplicate]

In Java, how would I line up my file/sizes so that both columns start at the exact same column position?

Here is my code thus far:

System.out.println(child.getName() + " \t\t" + child.length() + " , ");

it makes this output(pic):

enter image description here

However, I would like both columns to line up so that it's like a table-of-contents in a book.

Do I use format-specifierrs?

thanks

like image 400
Caffeinated Avatar asked Nov 18 '25 22:11

Caffeinated


1 Answers

System.out.printf("%40s %d,%n",child.getName(),child.length());

Where 40 is the minimum width. Change this as you see fit.

Edit: using %n instead of \n for OS specific linebreak.

like image 127
KevinL Avatar answered Nov 20 '25 11:11

KevinL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!