Introduction
The Formatter
class in Java, part of the java.util
package, provides support for formatting strings, numbers, dates, and other objects. It is designed to mimic the functionality of printf
in languages like C.
Table of Contents
- What is the
Formatter
Class? - Common Methods
- Formatting Syntax
- Examples of Using the
Formatter
Class - Conclusion
1. What is the Formatter Class?
The Formatter
class provides methods to format and output strings. It uses format specifiers to control the formatting of different types of data.
2. Common Methods
format(String format, Object... args)
: Writes a formatted string to this object's destination.flush()
: Flushes the formatter.close()
: Closes the formatter.
3. Formatting Syntax
The format string consists of fixed text and format specifiers. The format specifiers begin with a %
and end with a conversion character. Common format specifiers include:
%d
: Decimal integer%f
: Floating-point number%s
: String%t
: Date/Time%x
: Hexadecimal integer%e
: Scientific notation
Flags, width, precision, and argument index can modify the format specifiers:
- Flags:
-
(left-justify),+
(include sign),0
(zero-padding),,
(grouping separator) - Width: Specifies the minimum number of characters to be written
- Precision: Specifies the number of digits after the decimal point for floating-point numbers, or the maximum length of strings
- Argument Index: Specifies the position of the argument in the argument list
4. Examples of Using the Formatter Class
Example 1: Formatting Strings
This example demonstrates how to format strings using the Formatter
class.
import java.util.Formatter;
public class StringFormatExample {
public static void main(String[] args) {
Formatter formatter = new Formatter();
formatter.format("Hello, %s!", "world");
System.out.println(formatter);
formatter.close();
}
}
Output:
Hello, world!
Example 2: Formatting Numbers
This example shows how to format integers and floating-point numbers.
import java.util.Formatter;
public class NumberFormatExample {
public static void main(String[] args) {
Formatter formatter = new Formatter();
// Integer formatting
formatter.format("Integer: %d%n", 12345);
// Floating-point formatting
formatter.format("Floating-point: %.2f%n", 12345.6789);
// Hexadecimal formatting
formatter.format("Hexadecimal: %x%n", 255);
System.out.println(formatter);
formatter.close();
}
}
Output:
Integer: 12345
Floating-point: 12345.68
Hexadecimal: ff
Example 3: Formatting Dates
This example demonstrates how to format dates and times.
import java.util.Formatter;
import java.util.Calendar;
public class DateFormatExample {
public static void main(String[] args) {
Formatter formatter = new Formatter();
Calendar calendar = Calendar.getInstance();
// Date formatting
formatter.format("Date: %tD%n", calendar);
// Time formatting
formatter.format("Time: %tT%n", calendar);
// Full date/time formatting
formatter.format("Full date/time: %tc%n", calendar);
System.out.println(formatter);
formatter.close();
}
}
Output:
Date: 07/01/24
Time: 12:07:21
Full date/time: Mon Jul 01 12:07:21 IST 2024
Example 4: Using Flags
This example shows how to use flags in format specifiers.
import java.util.Formatter;
public class FlagsFormatExample {
public static void main(String[] args) {
Formatter formatter = new Formatter();
// Left-justify
formatter.format("Left-justified: %-10d%n", 123);
// Include sign
formatter.format("Include sign: %+d%n", 123);
// Zero-padding
formatter.format("Zero-padded: %010d%n", 123);
// Grouping separator
formatter.format("Grouping separator: %,d%n", 123456789);
System.out.println(formatter);
formatter.close();
}
}
Output:
Left-justified: 123
Include sign: +123
Zero-padded: 0000000123
Grouping separator: 123,456,789
Example 5: Using Width and Precision
This example demonstrates how to use width and precision in format specifiers.
import java.util.Formatter;
public class WidthPrecisionExample {
public static void main(String[] args) {
Formatter formatter = new Formatter();
// Width
formatter.format("Width: %10d%n", 123);
// Precision for floating-point
formatter.format("Precision: %.4f%n", 123.456789);
// Width and precision
formatter.format("Width and precision: %10.4f%n", 123.456789);
System.out.println(formatter);
formatter.close();
}
}
Output:
Width: 123
Precision: 123.4568
Width and precision: 123.4568
Example 6: Using Argument Index
This example shows how to use argument index to reuse arguments in the format string.
import java.util.Formatter;
public class ArgumentIndexExample {
public static void main(String[] args) {
Formatter formatter = new Formatter();
// Argument index
formatter.format("Reused arguments: %1$d %2$d %1$d%n", 123, 456);
System.out.println(formatter);
formatter.close();
}
}
Output:
Reused arguments: 123 456 123
5. Conclusion
The Formatter
class in Java provides a powerful and flexible way to format strings, numbers, dates, and other objects. By using format specifiers and modifiers, you can create well-formatted output for various types of data. The examples provided demonstrate common usage patterns and highlight the capabilities of the Formatter
class.
Comments
Post a Comment
Leave Comment