Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Guide to java.util.Formatter

$
0
0

1. Overview

In this article, we’ll discuss the String formatting in Java using the java.util.Formatter class, which provides support for the layout justification and alignment.

2. How to Use the Formatter

Remember C’s printf? Formatting a String in Java feels very similar.

The format() method of the Formatter is exposed via a static method from the String class. This method accepts a template String and a list of arguments to populate the template with:

String greetings = String.format(
  "Hello Folks, welcome to %s !", 
  "Baeldung");

The resulting String is:

"Hello Folks, welcome to Baeldung !"

A template is a String that contains some static text and one or more format specifiers, which indicate which argument is to be placed at the particular position.

In this case, there’s a single format specifier %s, which gets replaced by the corresponding argument.

3. Format Specifiers

3.1. General Syntax

The syntax of format specifiers for General, Character, and Numeric type is:

%[argument_index$][flags][width][.precision]conversion

Specifiers argument_index, flag, width, and precision are optional. 

  • argument_index part is an integer i – indicating that the ith argument from the argument list should be used here
  • flags is a set of characters used for modifying the output format
  • width is a positive integer which indicates the minimum number of characters to be written to the output
  • precision is an integer usually used to restrict the number of characters, whose specific behavior depends on the conversion
  • is the mandatory part. It’s a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument’s data type

In our example above, if we want to specify the number of an argument explicitly, we can write it using 1$ and 2$ argument indices.

Both these being the first and second argument respectively:

String greetings = String.format(
  "Hello %2$s, welcome to %1$s !", 
  "Baeldung", 
  "Folks");

3.2. For Date/Time Representation

%[argument_index$][flags][width]conversion

Again the argument_index, flags, and width are optional.

Let’s take an example to understand this:

@Test
public void whenFormatSpecifierForCalendar_thenGotExpected() {
    Calendar c = new GregorianCalendar(2017, 11, 10);
    String s = String.format(
      "The date is: %tm %1$te,%1$tY", c);

    assertEquals("The date is: 12 10,2017", s);
}

Here, for every format specifier, the 1st argument will be used, hence 1$. Here if we skip the argument_index for 2nd and 3rd format specifier, it tries to find 3 arguments, but we need to use the same argument for all 3 format specifiers.

So, it’s ok if we don’t specify argument _index for the first one, but we need to specify it for the other two.

The flag here is made up of two characters. Where the first character is always a ‘t’ or ‘T’. The second character depends on what part of Calendar is to be displayed.

In our example, the first format specifiers tm, indicates month formatted as two digits, te indicates the day of the month and tY indicated Year formatted as four digits.

3.3. Format Specifiers Without Arguments

%[flags][width]conversion

The optional flags and width are the same as defined in above sections.

The required conversion is a character or String indicating content to be inserted in the output. Currently, only the ‘%’ and newline ‘n’ can be printed using this:

@Test
public void whenNoArguments_thenExpected() {
    String s = String.format("John scored 90%% in Fall semester");
 
    assertEquals("John scored 90% in Fall semester", s);
}

Inside format(), if we want to print ‘%’ – we need to escape it by using ‘%%’.

4. Conversions

Let’s now dig into every detail of the Format Specifier syntax, starting with a conversion. Note that you can find all the details in the Formatter javadocs.

As we noticed in the above examples, conversion part is required in all format specifiers, and it can be divided into several categories.

Let’s take a look at each one by taking examples.

4.1. General 

Used for any argument type. The general conversions are:

  1. ‘b’ or ‘B’ – for Boolean values
  2. ‘h’ or ‘H’ – for HashCode
  3. ‘s’ or ‘S’ – for String, if null, it prints “null”, else arg.toString()

We’ll now try to display boolean and String values, using the corresponding conversions:

@Test
public void givenString_whenGeneralConversion_thenConvertedString() {
    String s = String.format("The correct answer is %s", false);
    assertEquals("The correct answer is false", s);

    s = String.format("The correct answer is %b", null);
    assertEquals("The correct answer is false", s);

    s = String.format("The correct answer is %B", true);
    assertEquals("The correct answer is TRUE", s);
}

4.2. Character 

Used for the basic types which represent Unicode characters: char, Character, byte, Byte, short, and ShortThis conversion can also be used for the types int and Integer when the Character.isValidCodePoint(int) returns true for them.

It can be written as ‘c’ or ’C’ based on the case we want.

Let’s try to print some characters:

@Test
public void givenString_whenCharConversion_thenConvertedString() {
    String s = String.format("The correct answer is %c", 'a');
    assertEquals("The correct answer is a", s);

    s = String.format("The correct answer is %c", null);
    assertEquals("The correct answer is null", s);

    s = String.format("The correct answer is %C", 'b');
    assertEquals("The correct answer is B", s);

    s = String.format("The valid unicode character: %c", 0x0400);
    assertTrue(Character.isValidCodePoint(0x0400));
    assertEquals("The valid unicode character: Ѐ", s);
}

Let’s take one more example of an invalid code point:

@Test(expected = IllegalFormatCodePointException.class)
public void whenIllegalCodePointForConversion_thenError() {
    String s = String.format("The valid unicode character: %c", 0x11FFFF);
 
    assertFalse(Character.isValidCodePoint(0x11FFFF));
    assertEquals("The valid unicode character: Ā", s);
}

4.3. Numeric – Integral

These are used for Java integral types: byte, Byte, short, Short, int and Integer, long, Long, and BigIntegerThere are three conversions in this category:

  1. ‘d’ – for decimal number
  2. ‘o’ – for octal number
  3. ‘X’ or ‘x’ – for hexadecimal number

Let’s try to print each of these:

@Test
public void whenNumericIntegralConversion_thenConvertedString() {
    String s = String.format("The number 25 in decimal = %d", 25);
    assertEquals("The number 25 in decimal = 25", s);

    s = String.format("The number 25 in octal = %o", 25);
    assertEquals("The number 25 in octal = 31", s);

    s = String.format("The number 25 in hexadecimal = %x", 25);
    assertEquals("The number 25 in hexadecimal = 19", s);
}

4.4. Numeric – Floating Point

Used for Java floating-point types: float, Float, double, Double, and BigDecimal

  1. ‘e’ or ‘E’ – formatted as a decimal number in computerized scientific notation
  2. ‘f’ – formatted as a decimal number
  3. ‘g’ or ‘G’based on the precision value after rounding, this conversion formats into computerized scientific notation or decimal format

Let’s try to print the floating point numbers:

@Test
public void whenNumericFloatingConversion_thenConvertedString() {
    String s = String.format(
      "The computerized scientific format of 10000.00 "
      + "= %e", 10000.00);
 
    assertEquals(
      "The computerized scientific format of 10000.00 = 1.000000e+04", s);
    
    String s2 = String.format("The decimal format of 10.019 = %f", 10.019);
    assertEquals("The decimal format of 10.019 = 10.019000", s2);
}

4.5. Other Conversions 

  • Date/Time – for Java types which are capable of encoding a date or time: long, Long, Calendar, Date and TemporalAccessor. For this, we need to use prefixed ‘t’ or ‘T’, as we saw earlier
  • Percent – prints a literal ‘%’ (‘\u0025’)
  • Line Separator – prints a platform-specific line separator

Let’s have a look at a simple example:

@Test
public void whenLineSeparatorConversion_thenConvertedString() {
    String s = String.format("First Line %nSecond Line");
 
    assertEquals("First Line \n" + "Second Line", s);
}

5. Flags

Flags, in general, are used to format the output. Whereas in case of date and time, they are used to specify which part of the date is to be displayed, as we saw in the Section 4 example.

A number of flags are available, a list of which can be found in the documentation.

Let’s see a flag example to understand it’s usage. ‘-‘ is used to format the output as left justified: 

@Test
public void whenSpecifyFlag_thenGotFormattedString() {
    String s = String.format("Without left justified flag: %5d", 25);
    assertEquals("Without left justified flag:    25", s);

    s = String.format("With left justified flag: %-5d", 25);
    assertEquals("With left justified flag: 25   ", s);
}

6. Precision

For general conversions, precision is just the maximum number of characters to be written to the output. Whereas, for the floating-point conversions the precision is the number of digits after the radix point.

The first statement is an example of precision with floating-point numbers, and the second one with general conversions:

@Test
public void whenSpecifyPrecision_thenGotExpected() {
    String s = String.format(
      "Output of 25.09878 with Precision 2: %.2f", 25.09878);
 
    assertEquals("Output of 25.09878 with Precision 2: 25.10", s);

    String s2 = String.format(
      "Output of general conversion type with Precision 2: %.2b", true);
 
    assertEquals("Output of general conversion type with Precision 2: tr", s2);
}

7. Argument Index

As mentioned previously, the argument_index is an integer that indicates the position of the argument in the argument list1$ indicates the first argument, 2$ the second argument, and so on.

Also, there is another way to reference arguments by position, by using the ‘<‘ (‘\u003c’) flag, which means the argument from the previous format specifier will be re-used. For example, these two statements would produce the identical output:

@Test
public void whenSpecifyArgumentIndex_thenGotExpected() {
    Calendar c = Calendar.getInstance();
    String s = String.format("The date is: %tm %1$te,%1$tY", c);
    assertEquals("The date is: 12 10,2017", s);

    s = String.format("The date is: %tm %<te,%<tY", c);
    assertEquals("The date is: 12 10,2017", s);
}

8. Other Ways of Using Formatter

Till now we saw the use of format() method of the Formatter class. We can also create a Formatter instance, and use that to invoke the format() method.

We can create an instance by passing in an Appendable, OutputStream, File or file name. Based on this, the formatted String is stored in an Appendable, OutputStream, File respectively.

Let’s see an example of using it with an Appendable. We can use it with others in the same way.

8.1. Using Formatter with Appendable

Let’s create a StringBuilder instance sb, and create a Formatter using it. Then we’ll invoke format() to format a String:

@Test
public void whenCreateFormatter_thenFormatterWithAppendable() {
    StringBuilder sb = new StringBuilder();
    Formatter formatter = new Formatter(sb);
    formatter.format("I am writting to a %s Instance.", sb.getClass());
    
    assertEquals(
      "I am writting to a class java.lang.StringBuilder Instance.", 
      sb.toString());
}

9. Conclusion

In this article, we saw the formatting facilities provided by the java.util.Formatter class. We saw various syntax that can be used to format the String and the conversion types that can be used for different data types.

As usual, the code for the examples we saw can be found over on Github.


Viewing all articles
Browse latest Browse all 4535

Trending Articles