
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Table with Columns in Java using Formatter
In this article, we will learn to display a table with columns in the output using Formatter in Java. The table will have headings and show different calculations of an array of floating-point numbers. The first column will display the original number, the second will display the number rounded up, and the third will display the number rounded down.
Problem Statement
Write a Java program to display a table with columns in the output using Formatter.
Input
double arr[] = { 1.7, 2.5, 3.1, 4.5, 5.7, 6.9, 7.7, 8.9, 9.1 };
Output
The point list...
Points1 Points2 Points3
1.70 2.00 1.00
2.50 3.00 2.00
3.10 4.00 3.00
4.50 5.00 4.00
5.70 6.00 5.00
6.90 7.00 6.00
7.70 8.00 7.00
8.90 9.00 8.00
9.10 10.00 9.00
Steps to display table with columns using Formatter
Following are the steps to display a table with columns using Formatter ?
- We will start by importing the Formatter class from java.util package.
- Create an array of double values to use as the data.
- After that initialize a Formatter object to help format the output.
- Use the format() method to create the table's header with three columns.
- Iterate over the array of numbers and use the format() method again to format each row, showing the original, ceiling, and floor values.
- Display the formatted table in the output.
Java program to display table with columns using Formatter
Below is an example of displaying a table with columns in the output using Formatter ?
import java.util.Formatter; public class Demo { public static void main(String[] argv) throws Exception { double arr[] = { 1.7, 2.5, 3.1, 4.5, 5.7, 6.9, 7.7, 8.9, 9.1 }; Formatter f = new Formatter(); f.format("%15s %15s %15s\n", "Points1", "Points2", "Points3"); System.out.println("The point list...\n"); for (double d : arr) { f.format("%14.2f %14.2f %15.2f\n", d, Math.ceil(d), Math.floor(d)); } System.out.println(f); } }
Output
The point list... Points1 Points2 Points3 1.70 2.00 1.00 2.50 3.00 2.00 3.10 4.00 3.00 4.50 5.00 4.00 5.70 6.00 5.00 6.90 7.00 6.00 7.70 8.00 7.00 8.90 9.00 8.00 9.10 10.00 9.00
Code Explanation
In this program, we first import the Formatter class and define an array of floating-point numbers. A Formatter object is initialized to help format the output into a table with columns. We use the format() method to set up the column headers as "Points1," "Points2," and "Points3." Then, for each number in the array, we calculate its ceiling and floor values using Math.ceil() and Math.floor() and display them alongside the original number in the respective columns. The format() method ensures that each value is aligned correctly with two decimal points. Finally, the formatted output is printed to the console.