Output in a table format in Java's System.out
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).
Using j-text-utils you may print to console a table like:
And it as simple as:
TextTable tt = new TextTable(columnNames, data);
tt.printTable();
The API also allows sorting and row numbering ...
I've created a project that can build much advanced table views. If you supposed to print the table, the width of the table going to have a limit. I have applied it in one of my own project to get a customer invoice print. Following is an example of the print view.
PLATINUM COMPUTERS(PVT) LTD
NO 20/B, Main Street, Kandy, Sri Lanka.
Land: 812254630 Mob: 712205220 Fax: 812254639
CUSTOMER INVOICE
+-----------------------+----------------------+
|INFO |CUSTOMER |
+-----------------------+----------------------+
|DATE: 2015-9-8 |ModernTec Distributors|
|TIME: 10:53:AM |MOB: +94719530398 |
|BILL NO: 12 |ADDRES: No 25, Main St|
|INVOICE NO: 458-80-108 |reet, Kandy. |
+-----------------------+----------------------+
| SELLING DETAILS |
+-----------------+---------+-----+------------+
|ITEM | PRICE($)| QTY| VALUE|
+-----------------+---------+-----+------------+
|Optical mouse | 120.00| 20| 2400.00|
|Gaming keyboard | 550.00| 30| 16500.00|
|320GB SATA HDD | 220.00| 32| 7040.00|
|500GB SATA HDD | 274.00| 13| 3562.00|
|1TB SATA HDD | 437.00| 11| 4807.00|
|RE-DVD ROM | 144.00| 29| 4176.00|
|DDR3 4GB RAM | 143.00| 13| 1859.00|
|Blu-ray DVD | 94.00| 28| 2632.00|
|WR-DVD | 122.00| 34| 4148.00|
|Adapter | 543.00| 28| 15204.00|
+-----------------+---------+-----+------------+
| RETURNING DETAILS |
+-----------------+---------+-----+------------+
|ITEM | PRICE($)| QTY| VALUE|
+-----------------+---------+-----+------------+
|320GB SATA HDD | 220.00| 4| 880.00|
|WR-DVD | 122.00| 7| 854.00|
|1TB SATA HDD | 437.00| 7| 3059.00|
|RE-DVD ROM | 144.00| 4| 576.00|
|Gaming keyboard | 550.00| 6| 3300.00|
|DDR3 4GB RAM | 143.00| 7| 1001.00|
+-----------------+---------+-----+------------+
GROSS 59,928.00
DISCOUNT(5%) 2,996.40
RETURN 9,670.00
PAYABLE 47,261.60
CASH 20,000.00
CHEQUE 15,000.00
CREDIT(BALANCE) 12,261.60
--------------------- ---------------------
CASH COLLECTOR GOODS RECEIVED BY
soulution by clough.com
This is the code for above print view and you can find the library (Wagu) in here.