
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!
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
- How to find version of Cargo in Rust - Rust
- Examples: Convert String to int in JavaScript - JavaScript
- How to remove quotes from a String in Python - Python
- Display full website URL/address in Safari macOS Browser - MacOS
- SharePoint workflow Canceled - Coercion Failed: Unable to transform the input lookup data into the requested type - SharePoint
- What does apt-get update command does? - Linux
- Python Comments Multiple Lines - Python
- How to Send or Publish SNS Message using AWS CLI - AWS