Java Format Specifier

In this chapter you will learn:

  1. What is Format Specifier
  2. What are the specifiers for Java Formatter class
  3. Example - Java Format Specifier
  4. Example - Unknown Format Conversion Exception
  5. How to use %n and %% specifiers to escape new line and percentage
  6. Example - The following code combines the %n, %d %% to display file copy progress information

Description

The format() method accepts a wide variety of format specifiers. When an uppercase specifier is used, then letters are shown in uppercase. Otherwise, the upper- and lowercase specifiers perform the same conversion.

Specifier List

The following table shows the format specifiers:

Format SpecifierConversion Applied
%a %AFloating-point hexadecimal
%b %BBoolean
%cCharacter
%dDecimal integer
%h %HHash code of the argument
%e %EScientific notation
%fDecimal floating-point
%g %GUses %e or %f, whichever is shorter
%oOctal integer
%nInserts a newline character
%s %SString
%t %TTime and date
%x %XInteger hexadecimal
%%Inserts a % sign

If the argument doesn't match the type-checks, an IllegalFormatException is thrown.

Example

Java Format Specifier


import java.util.Calendar;
import java.util.Formatter;
/*w  w  w  .  j  ava2 s .  c om*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();

    fmt = new Formatter();
    System.out.println(fmt.format("%s gap filler %d %f", "Astring", 10, 12.3)); 
  
  }
}

You can obtain a reference to the underlying output buffer by calling out(). It returns a reference to an Appendable object.

The code above generates the following result.

Example 2

Unknown Format Conversion Exception


import java.util.Date;
import java.util.Formatter;
/*from w w w. java2s.c o  m*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();

    fmt.format("%t", new Date());
    System.out.println(fmt);
  }
}

The code above generates the following result.

Example 3

The %n and %% format specifiers escape sequences. The %n inserts a newline. The %% inserts a percent sign.

You can use the standard escape sequence \n to embed a newline character. Here is an example that demonstrates the %n and %% format specifiers:


import java.util.Formatter;
/*w w w . j  a v  a 2  s .  c o  m*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("line%nline %d%% complete", 88);
    System.out.println(fmt);
  }
}

The output:

Example 4

The following code combines the %n, %d %% to display file copy progress information.


import java.util.Formatter;
//from w  ww .jav  a2 s.c  om
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("Copying file%nTransfer is %d%% complete", 88);
    System.out.println(fmt);

  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is Format Specifier
  2. How and when to use Uppercase Option for Java Formatter
  3. How to uppercase hexadecimal
  4. How to uppercase e symbol
Home »
  Java Tutorial »
    Java Langauge »
      Java Data Format
Java Formatter Class
Java Format Specifier
Java Format Specifier Uppercase
Java Format Precision
Java Format Flags
Java Format Justifying Output
Java Format Negative and Positive
Java Format Line up Space
Java Format Parentheses
Java Format Zero Padding
Java Format Comma
Java Format Alternate Conversion
Java Format Argument Index
Java Format date time
Java Format Date