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

前端 未结 8 1416
清歌不尽
清歌不尽 2020-11-22 15:03

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 vari

相关标签:
8条回答
  • 2020-11-22 15:54
    public class Main {
     public static void main(String args[]) {
       String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
       System.out.format(format, "A", "AA", "AAA");
       System.out.format(format, "B", "", "BBBBB");
       System.out.format(format, "C", "CCCCC", "CCCCCCCC");
    
       String ex[] = { "E", "EEEEEEEEEE", "E" };
    
       System.out.format(String.format(format, (Object[]) ex));
     }
    }
    

    differece in sizes of input doesnt effect the output

    0 讨论(0)
  • 2020-11-22 15:56

    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.

    0 讨论(0)
提交回复
热议问题