Tuesday 13 July 2021

AsciiTable: Hello world app: Create table

Step 1: Get an instance of AsciiTable.

AsciiTable asciiTable = new AsciiTable();

 

Step 2: Add rows the ascii table.

asciiTable.addRule();
asciiTable.addRow("row 1 col 1", "row 1 col 2");
asciiTable.addRule();
asciiTable.addRow("row 2 col 1", "row 2 col 2");
asciiTable.addRule();

 

‘addRule’ method adds a rule row to the table using the default style.

 

'addRow' method takes array of columns as argument and adds the content row to the table. When the first row is added to asciiTable, it identifies the number of columns in the table based on row content. In the above example, asciiTable has two columns.

 

Step 3: Render an ascii table.

String rend = asciiTable.render();


Step 4: Print the rendered content.

System.out.println(rend);


Find the below working application.

 

HelloWorld.java

package com.sample.app;

import de.vandermeer.asciitable.AsciiTable;

public class HelloWorld {

	public static void main(String args[]) {
		AsciiTable asciiTable = new AsciiTable();

		asciiTable.addRule();
		asciiTable.addRow("row 1 col 1", "row 1 col 2");
		asciiTable.addRule();
		asciiTable.addRow("row 2 col 1", "row 2 col 2");
		asciiTable.addRule();
		
		String rend = asciiTable.render();
				
		System.out.println(rend);

	}

}


Output

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment