MessageFormat format() method in Java with Example : Set – 1

The format() method of java.text.MessageFormat class is used to get the formatted array of object appended into the string buffer object. formatted array will contain all forms of element lies in the pattern of MessageFormat object.

Syntax:

public final StringBuffer format(Object[] arguments,
                                 StringBuffer result,
                                 FieldPosition pos)

Paramter:

  • aragument :– This method takes array object as a parameter for which formatting is going to take place.
  • result :- string buffer will be use for appending the formatted array.
  • pos :- field position will be going to use for alignment purpose.

Return Value: This method returns string buffer which will have the appended result of formatted array.

Exception: This method throws NullPointerException if the result is null.



Below are the examples to illustrate the format() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// format() method
  
import java.text.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing new MessageFormat Object
            MessageFormat mf
                = new MessageFormat("{0, number, #}, {0, number, #.##}, {0, number}");
  
            // Creating and initializing new FieldPosition Object
            FieldPosition fp
                = new FieldPosition(MessageFormat.Field.ARGUMENT);
  
            // Creating and initializing an array of type Double
            // to be formated
            Object[] objs = { new Double(9.5678) };
  
            // Creating and initializing StringBuffer for
            // appending the result
            StringBuffer stb = new StringBuffer(10);
  
            // Formating an array of object
            // using format() method
            stb = mf.format(objs, stb, fp);
  
            // display the result
            System.out.println("formated array : "
                               + stb.toString());
        }
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

formated array : 10, 9.57, 9.568

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// format() method
  
import java.text.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing new MessageFormat Object
            MessageFormat mf
                = new MessageFormat("{0, number, #}, {0, number, #.##}, {0, number}");
  
            // Creating and initializing new FieldPosition Object
            FieldPosition fp
                = new FieldPosition(MessageFormat.Field.ARGUMENT);
  
            // Creating and initializing an array of type Double
            // to be formated
            Object[] objs = { new Double(9.5678) };
  
            // Creating and initializing StringBuffer for
            // appending the result
            StringBuffer stb = new StringBuffer(10);
  
            // Formating an array of object
            // using format() method
            stb = mf.format(objs, null, fp);
  
            // display the result
            System.out.println("formated array : "
                               + stb.toString());
        }
        catch (NullPointerException e) {
            System.out.println("StringBuffer is null " + e);
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

old pattern : {0, date, #}, {1, date, #}, {0, number}

String is Null
StringBuffer is null java.lang.NullPointerException
Exception thrown : java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#format-java.lang.Object:A-java.lang.StringBuffer-java.text.FieldPosition-

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.