Formatter Argument Index

In this chapter you will learn:

  1. Using an Argument Index
  2. How to reuse the parameter
  3. How to use relative index
  4. How to use relative index to create customized date format

Using an Argument Index

Formatter can have the argument to indicate a format specifier index. By using an argument index, you can control which argument a format specifier matches.

An argument index immediately follows the % in a format specifier. It has the following format:

n$

where n is the index of the desired argument, beginning with 1. For example, consider this example:

import java.util.Formatter;
//  j  a  va  2  s. c om
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%3$d %1$d %2$d", 10, 20, 30); 
    System.out.println(fmt);
  }
}

The output:

Reuse the parameter

The following code uses the argument index to reuse the parameter.

import java.util.Formatter;
/*from  j  av  a  2 s.co  m*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%d in hex is %1$x", 255); 
    System.out.println(fmt);
  }
}

Relative index

A relative index, <, enables you to reuse the argument matched by the preceding format specifier.

import java.util.Formatter;
//  j  av a 2 s . c  om
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%d in hex is %<x", 255); 
    System.out.println(fmt);
  }
}

The output:

Relative indexes and customized date format

Relative indexes are useful when creating custom time and date formats.

Because of relative indexing, the argument cal need only be passed once, rather than three times.

import java.util.Calendar;
import java.util.Formatter;
//from j a  v a 2  s  .co m
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();
    fmt.format("Today is day %te of %<tB, %<tY", cal);
    System.out.println(fmt);
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to right and left align strings
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words