![Java Console Table Example Java Console Table Example](https://code2care.org/java/display-output-in-java-console-as-a-table/images/Java Console Table Example.png)
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!
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- Json Serialization and Deserialization using Java Jackson - Java
- Capture cURL Request Output to a File - cURL
- How to Open a New Notebook in Google Colab - Google
- Share Image to WhatsApp with Caption from your Android App - WhatsApp
- Install OpenJDK Java (8/11/17) versions using brew on Mac (Intel/M1/M2) - MacOS
- Android App Showing Two Toolbars Issue fix - Android
- Float built-in function in Python - Python
- Get Absolute Relative Path of File in IDEA IntelliJ - Java