Javatpoint Logo
Javatpoint Logo

How to Print Table in Java Using Formatter?

Sometimes, we are required to represent data in tabular format or in table form, especially when we are dealing with the database. In this section, we will create Java programs that print data on the console in tabular or table format.

There are many ways to print tables through a Java program.

  • Using Java Formatter Class
  • Using Java PrintStream Class

Using Java Formatter Class

Java Formatter class belongs to java.util package. The class provides the format() method that prints the data in the table format.

Java Formatter.format() Method

The method is used to write the formatted string to the

Syntax:

Parameters:

l: Locale to apply during format. It is optional. If we do not specify the locale in the parameter, no localization is applied.

format: A format string.

args: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments may be 0. Note that we can pass the limited number of arguments.

Returns: It returns the formatter.

Exception Throws:

  • IllegalFormatException: If the format string contains illegal syntax.
  • FormatterClosedException: If the formatter is closed.

Let's use the print() and format() method to print tables through a Java program.

FormatterExample.java

Output:

How to Print Table in Java Using Formatter?

Using Java PrintStream Class

Java provides the PrintStream class that is used to print formatted output. The class belongs to the java.io package. There are many methods that can be used to print data on the console. But in this section we will use only two methods of the class i.e., printf() and format() method.

Java PrintStream.printf() Method

The printf() method is used to write the formatted string to this output stream. The invocation of the method is the same as:

Syntax:

Parameters:

l: Locale to apply during format. It is optional. If we do not specify the locale in the parameter then no localization is applied.

format: A format string.

args: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments may be 0. Note that we can pass the limited number of arguments.

Returns: It returns the output stream.

Exception Throws: It throws IllegalFormatException if the format string contains illegal syntax. It also throws NullPointerException if we have not specified the syntax.

Java PrintStream.format() Method

The format() method is used to write a formatted string to this output stream using the specified format string and arguments.

Syntax:

Parameters:

l: Locale to apply during format. It is optional. If we do not specify the locale in the parameter then no localization is applied.

format: A format string.

args: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments may be 0. Note that we can pass the limited number of arguments.

Returns: It returns the output stream.

Exception Throws: It throws IllegalFormatException if the format string contains illegal syntax. It also throws NullPointerException if we have not specified the syntax.

Let's use the printf() and format() method to print tables through a Java program.

Employee.java

In the above code, we have generated getters and setters. Now we will create the main class in which we will add some objects to the list that will display on the console in table format.

EmployeeMain.java

Output:

How to Print Table in Java Using Formatter?





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA