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

The format() method of java.text.MessageFormat class is used to get the formatted array of object according to the specified pattern of message format object. new string pattern will be considered while performing the action.

Syntax:

public static String format(String pattern,
                            Object... arguments)

Parameter: This method takes following argument as a parameter.

  • pattern :– string pattern according to which array of object will be formatted
  • arguments :- array of object over which formatting is going to take place.

Return Value: This method returns string value which will have the formatted array of object in string formate.

Exception: This method throws NullPointerException if pattern 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 an array of type Double
            // to be formatted
            Object[] objs = { new Double(4.234567) };
  
            // Formatting an array of object
            // using format() method
            String str = mf.format("{0, number, #.#}", objs);
  
            // display the result
            System.out.println("formatted array : "
                               + str);
        }
        catch (NullPointerException e) {
            System.out.println("pattern is null " + e);
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

formatted array : 4.2

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 an array of type Double
            // to be formatted
            Object[] objs = { new Double(4.234567) };
  
            // Formatting an array of object
            // using format() method
            String str = mf.format(null, objs);
  
            // display the result
            System.out.println("formatted array : "
                               + str);
        }
        catch (NullPointerException e) {
            System.out.println("pattern is null ");
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

pattern is null 
Exception thrown : java.lang.NullPointerException

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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.