M4tt3
M4tt3

Reputation: 55

How to format java output like a table

I have a data structure that i would like to print on the output like a table.

i've usedSystem.Out printf method but the result is not good.

System.out.printf("%4s  %-7s   %-4s   %-7s%n", "node", "node Ip", "Type", "subType");
System.out.printf("%2d  %-7s   %-12s   %-21s%n", node.getNodeKey(),
              node.getNodeIpAddress(), 
              node.getNodeType(), 
              node.getNodeSubType());

and the result is:

node  node Ip   Type   subType
 2  10.1.1.2   NODETYPE_WDM   NODESUBTYPE_RROADM_2 
 0            NODETYPE_NONE   NODESUBTYPE_NONE 

is there any smart way to do this well?

Thanks a lot.

Upvotes: 1

Views: 8010

Answers (3)

Sage
Sage

Reputation: 15408

If the data is already read inside a memory and we can work with them offline: means that data is not essentially appearing one by one, or if so it is possible to store them inside a table: a 2D array like data structure or collection to work with before-hand:

  1. we can try computing maximum width of each column
  2. Then use the maximum width as formatting flag: "%"+maxWidth[col]+"s";

I am providing a short working example for you:

String data[][] = { {"Id", "Vehicle", "Disance" },
                    {"1", "ByCycle", "2 km"}, 
                    {"2", "Car", "10 km"}, 
                    {"3", "Van", "6.5 km"} 
                  };

        int col = data[0].length;
        int row = data.length;

        int maxWidth[] = new int[col];

        for(String[] rowD : data)
         for(int i=0; i< col; i++)
         {
             if(maxWidth[i] < rowD[i].length())
                 maxWidth[i] = rowD[i].length();
         }

        String format = "";

        for(int x:maxWidth)
            format += "%-"+(x+2)+"s ";

        format +="%n";

        for(String[] rowD : data)
        {
            System.out.printf(format, rowD);
        }

The sample output will be:

Id   Vehicle   Disance   
1    ByCycle   2 km      
2    Car       10 km     
3    Van       6.5 km   

Upvotes: 1

CPerkins
CPerkins

Reputation: 9008

That depends on what you mean by "smart", I think.

But to continue with the way you're doing it, try two things:

1) Header row field widths must be the same size as data row widths; and
2) You'll want them to be big enough.

This shows you what I mean:

  public void printLineImproved (int node, String nodeIp, String nodeType, String nodeSubtype) {
    System.out.printf("%4s  %-12s   %-12s   %-21s%n", "node", "node Ip", "Type", "subType");
    System.out.printf("%4d  %-12s   %-12s   %-21s%n", node, nodeIp, nodeType, nodeSubtype);
  }

The output from that is:

node  node Ip        Type           subType              
   2  10.1.1.2       NODETYPE_WDM   NODESUBTYHPE_RROADM_2

Upvotes: 1

Akira
Akira

Reputation: 4071

Most straight forward way is to use tab characters "\t". But this is not guaranteed to work if the length of any printed string is larger than tab. Also, the width of tab depends on many factors.

The best way to accomplish what you need is by checking the maximum width of each column and add space characters as needed to make the layout beautiful.

Upvotes: 0

Related Questions