Print a Table in Java
-
Using
printf()
/println()
to Print a List as a Table in Java -
Using
System.out.format()
to Print a List as a Table in Java

To print any information in a tabular structure in Java, we can use the printf()
or format()
method of the class java.io.PrintStream
.
Using printf()
/println()
to Print a List as a Table in Java
The printf(format, arguments)
method provides String formatting. We can specify the rules for formatting using format patterns and the rules start with %
.
Here we have a POJO class Student
with basic attributes like id
, name
, age
and grade
and the TestExample
class where we create a List
of students and print that information in a tabular format.
The formatting string consists of literals and format specifiers, which include flags, width, precision, and conversion character in this sequence. For example %[flags][width][.precision]conversion-charcater
. Specifiers given in the bracket are optional. The printf
internally uses java.util.Formatter
to parse the format string and output it.
The conversion-character
determines how the string is formatted. Here we have used two of the common ones, s
, d
, and c
. The s
formats strings while d
formats decimal integers, and the result of c
is a Unicode Character. Hence, in this code, we have used a combination of width and conversion-character to format the given student’s data into a table.
The method printf("%10s %20s %5s %5s", "STUDENT ID", "NAME", "AGE", "GRADE")
has the format specifier to format the arguments passed. Thus, %10s
, for example, formats a string with a specified number of characters and also right justify it. The println()
method moves the cursor to the next line after printing the result.
The method format("%10s %20s %5d %5c",student.getId(), student.getName(), student.getAge(), student.getGrade())
also has the format specifier and getter methods of the student class to get the value of the attributes.
import java.util.ArrayList;
import java.util.List;
public class TableExample {
public static void main (String args[]){
List<Student> students = new ArrayList<>();
students.add(new Student("S101","John",8, '4'));
students.add(new Student("S102","Leo",10, '6'));
students.add(new Student("S103","Mary",5, '2'));
students.add(new Student("S104","Lisa",6, '3'));
System.out.println("-----------------------------------------------------------------------------");
System.out.printf("%10s %20s %5s %5s", "STUDENT ID", "NAME", "AGE", "GRADE");
System.out.println();
System.out.println("-----------------------------------------------------------------------------");
for(Student student: students){
System.out.format("%10s %20s %5d %5c",
student.getId(), student.getName(), student.getAge(), student.getGrade());
System.out.println();
}
System.out.println("-----------------------------------------------------------------------------");
}
}
class Student{
private String id;
private String name;
private int age;
private Character grade;
Student(String id,String name,int age, Character grade){
this.id = id;
this.name = name;
this.age = age;
this.grade = grade;
}
public String getId() {
return id;
}
public Character getGrade() {
return grade;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Output:
-----------------------------------------------------------------------------
STUDENT ID NAME AGE GRADE
-----------------------------------------------------------------------------
S101 John 8 4
S102 Leo 10 6
S103 Mary 5 2
S104 Lisa 6 3
-----------------------------------------------------------------------------
Using System.out.format()
to Print a List as a Table in Java
The java.io package
provides the PrintStream
class with two methods used to replace print
and println
. These methods are format()
and printf()
and are equivalent. In format(String format, Object... args)
, the format specifies the formatting to be used, args
, which are the list of arguments to be printed using this formatting.
Here, we create a 2D array of String
data type; the 4 rows and columns are not specified at this point. It simply means that we are declaring an array of 4 arrays. Then, we initialize each row with a String object.
We run a for loop for each row inside the table
, a multidimensional array of arrays. For each row, we format the row using System.out.format()
specifying the formatting pattern for each row.
Here %15s
means right-justified string with 15 widths, and %n
is a platform-specific line separator.
public class Test1 {
public static void main (String args[]){
String[][] table = new String[4][];
table[0] = new String[] { "Apple", "Banana", "Papaya" };
table[1] = new String[] { "Chicken", "Mutton", "Fish" };
table[2] = new String[] { "Carrots", "Beans", "Cabbage" };
table[3] = new String[] { "Candy", "Cake", "Bread" };
for (String[] row : table) {
System.out.format("%15s %15s %15s %n", row);
}
}
}
Output:
Apple Banana Papaya
Chicken Mutton Fish
Carrots Beans Cabbage
Candy Cake Bread
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java Print
- Print Contents of Text File to Screen in Java
- Print Variables in Java
- Java system.out.println() Method
- Print New Line in Java