Formatted Output in Java



In Java, the formatted output refers to a process of generating well-structured data by specifying how data should be displayed. It involves using format specifiers to control the appearance (visibility) of various data types, such as integers, floating-point numbers, strings, characters, and dates.

In this article, we will discuss two key concepts about formatted output in Java as follows:

Format Specifiers: Strings that define how some kinds of data should be formatted.

Flags and Width: Options to control the run of the output, including aligning it, setting its padding, and regulating its width.

Common Format Specifiers

Format specifiers in Java are the special characters and symbols used within format strings to define how different types of data should be formatted and displayed.

Some of the most frequently used format specifiers are as follows:

Specifier Description
%d Decimal integer
%f Floating-point number
%s String
%c Character
%x Hexadecimal integer
%tm Two-digit month (for dates)
%tY Four-digit year (for dates)

To display the formatted output in Java is quite easy. Let us learn the following methods:

Formatted Output using String format() Method

In Java, we have a format() method to retrieve the formatted output. This method belongs to the string class, which prints formatted data.

Example

In the following example, we will format and display the formatted output of the given data using the System.out.format() method. This method includes various format specifiers to handle different types of data such as floats, integers, strings, etc.

import java.util.Date;

public class StringFormatExample {
   public static void main(String[] args) {
      int number = 42;
      double pi = 3.14159;
      String text = "Hello, World!";
      char letter = 'A';
      int hexValue = 255;
      Date date = new Date();
      
      // Formatting strings
      System.out.format("Integer: %d%n", number);
      System.out.format("Floating-point: %.2f%n", pi); 
      System.out.format("String: %s%n", text);
      System.out.format("Character: %c%n", letter);
      System.out.format("Hexadecimal: %x%n", hexValue);
      System.out.format("Two-digit month: %tm%n", date);
      System.out.format("Four-digit year: %tY%n", date);
   }
}

Output

Following is the output of the above program ?

Integer: 42
Floating-point: 3.14
String: Hello, World!
Character: A
Hexadecimal: ff
Two-digit month: 12
Four-digit year: 2024

Formatted Output using printf() Method

Here, we have another way to format the given data using printf() method. The printf() method in Java is used to produce formatted data using format specifers.

Example

In this program, we use the System.out.printf() method to format various types of data by passing format specifiers such as %f, %s, %c, etc., to retrieve the formatted output.

import java.util.Date;

public class FormatSpecifiersExample {
   public static void main(String[] args) {
      int number = 42;
      double pi = 3.14159;
      String text = "Hello, World!";
      char letter = 'A';
      int hexValue = 255;
      Date date = new Date();
      
      // Displaying formatted output
      System.out.printf("Integer: %d%n", number);
      System.out.printf("Floating-point: %.2f%n", pi); 
      System.out.printf("String: %s%n", text);
      System.out.printf("Character: %c%n", letter);
      System.out.printf("Hexadecimal: %x%n", hexValue);
      System.out.printf("Two-digit month: %tm%n", date);
      System.out.printf("Four-digit year: %tY%n", date);
   }
}

Output

The above program produce the following result ?

Integer: 42
Floating-point: 3.14
String: Hello, World!
Character: AHexadecimal: ff
Two-digit month: 12
Four-digit year: 2024
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2024-12-26T20:45:17+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements