
If you want to print two-dimensional data just like a table in Java Console with headers, rows, columns, and borders, you can do it by using println function with formatting options - but do there are many external libraries (jars) that can make your life easy and one of them is called java-ascii-table
✌️You can download this project jar from here: https://code.google.com/archive/p/java-ascii-table/downloads
The usage of this library is very simple, create a 1-dimensional header array of Strings, and your data could be a 2-dimensional array. Using the instance of ASCIITable class you can print the table using printTable() function that takes in two parameters String[] header and String[][] data.
package org.code2care;
import com.bethecoder.ascii_table.ASCIITable;
public class JavaConsoleTableExample {
public static void main(String[] args) {
String [] tableHeaders = { "Employee Name", "Salary", "Designation","Department"};
String[][] tableData = {
{ "Mike Kurt", "10000", "Developer", "IT" },
{ "Steve Musk", "20000", "Lead DevOps", "IT" },
{ "Larry Jobs", "30000", "Java Developer", "IT" },
{ "Elon Peters", "400000", "Manager", "IT" },
{ "Jake Burg", "50000000", "CEO", "IT" },
};
ASCIITable.getInstance().printTable(tableHeaders, tableData);
}
}
Output:
+---------------+----------+----------------+------------+
| Employee Name | Salary | Designation | Department |
+---------------+----------+----------------+------------+
| Mike Kurt | 10000 | Developer | IT |
| Steve Musk | 20000 | Lead DevOps | IT |
| Larry Jobs | 30000 | Java Developer | IT |
| Elon Peters | 400000 | Manager | IT |
| Jake Burg | 50000000 | CEO | IT |
+---------------+----------+----------------+------------+
As you can see in the above output the table is rendered using dash (-), pipe (|), and plus (+) ASCII characters and it looks really amazing.
✌️Try and explore the printTable method that is overloaded with features that can be used to align the table cell data on console.
public void printTable(String[] header, String[][] data)
public void printTable(String[] header, String[][] data, int dataAlign)
public void printTable(String[] header, int headerAlign, String[][] data, int dataAlign)
public void printTable(IASCIITableAware asciiTableAware)
Have Questions? Post them here!
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
- How to add ruler in Sublime Text tab window - Sublime-Text
- How to switch from bash to zsh shell in macOS Terminal - MacOS
- Create Symbolic Link using Terminal Command - MacOS
- How to un-hide tab bar in notepad++ - NotepadPlusPlus
- How to make a dummy phone call from Android Emulator device - Android
- How to enable anonymous public access for SharePoint Online site collection, file, folder without login ? - SharePoint
- [macOS] NetBeans IDE cannot be installed. Java (JRE) found on your computer but JDK XX or newer is required. - MacOS
- How to Identify installed PowerShell version - Powershell