Java Format - Java Formatter(Appendable a) Constructor


Formatter(Appendable a) constructor from Formatter has the following syntax.

public Formatter(Appendable a)


In the following code shows how to use Formatter.Formatter(Appendable a) constructor.

import java.util.Formatter;
import java.util.Locale;
//  w w w.ja va2 s.  c o m
public class Main {

   public static void main(String[] args) {

      StringBuffer buffer = new StringBuffer();
      Formatter formatter = new Formatter(buffer);

      // format a new string
      String name = "from";
      formatter.format("Hello %s !", name);

      // print the formatted string

      // flush the formatter. Here it does nothing.
      System.out.println("Formatter Flushed.");

The code above generates the following result.