Java | Printing to CONSOLE in TABLE format | Simple code with Flexible width, left align, horizontal lines, Cell data wrap

In this article, we will go through very simple code to print tabular format to console using simple pure java code.

Steps to print table:

  • Simple table:
    • Table data – Take table data in 2 dimensional array format. Each sub-array is a row. First sub-array will be header row.
    • Column Lengths – For each column, check length of all strings in that column. Take max string length in that column as column width for that column.
    • Format string – Prepare format string for printf method using column widths calculated in earlier step. Refer Formatter Documentation
    • Left Justify – This format will also consider a left-justify boolean flag to prepare format string to left justify each row.
    • Print Table – Iterate through array & print table row by row using System.out.printf  with format prepared in earlier step.
  • Additional features
    • Beautify with horizontal lines – We will also add line at top, bottom & below header line to make it look more reader friendly.
    • Cell wrapping – If certain cell value is more than certain fixed max width, then wrap the data of that cell to next line.



Simple table

This is a simple code to print table with all step above except horizontal lines & cell wrapping. This table has feature to auto adjust width as per the data.

Output

With leftJustifiedRows = true




With longer width data. Column widths will adjust as per data.



Table with horizontal lines

Now we will modify above code to include horizontal lines at top & bottom of table & also below header line so that header appears separately.



Output

With leftJustifiedRows = true

With longer width data. Column widths will adjust as per data.



Table with horizontal lines & Cell data wrapping

Now we will add feature for wrapping data in a table cell. We will define a ‘max width’ for cell. If any cell data is more than defined ‘max width’ then that data will be cropped & put to next line.

This is bit tricky as it takes certain manipulation of original table data i.e. 2-dimensional array. We will iterate through original table array & if we find any data more than ‘max width’, then we will add extra row in array where we will put cropped data after ‘max width’. This way we will prepare a new 2-dimensional array with final wrapped data & use that as a input to the table. Here is the code.



Output



2 Replies to “Java | Printing to CONSOLE in TABLE format | Simple code with Flexible width, left align, horizontal lines, Cell data wrap”

  1. The method iterate(T, UnaryOperator) in the type Stream is not applicable for the arguments (int, (( i) -> {}), (( i) -> {}))

    I am using Java 8 .. Getting this error on Stream.Iterator can you please help, fixing this

    1. Iterate method with 3 arguments was introduced in java 9. Below is api

      https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html#iterate(T,java.util.function.Predicate,java.util.function.UnaryOperator)

      For jdk 8 or less, just replace

      static  Stream iterate​(T seed, Predicate hasNext, UnaryOperator next)

      with simple for loop

      for (T index=seed; hasNext.test(index); index = next.apply(index)) { … }

      Both have same effect. Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *