MessageFormat format() method in Java with Example : Set - 2 Last Updated : 04 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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 formattedarguments :- 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 format.Exception: This method throws NullPointerException if pattern is null.Below are the examples to illustrate the format() method:Example 1: Java // 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); } } } Output: formatted array : 4.2 Example 2: Java // 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); } } } 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...- Comment More infoAdvertise with us Next Article MessageFormat format() method in Java with Example : Set - 2 R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-text package Java-MessageFormat Practice Tags : Java Similar Reads 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, StringB 3 min read MessageFormat getFormats() method in Java with Example The getFormats() method of java.text.MessageFormat class is used to get the formats used for previously set pattern for the message format object.Syntax: public Format[] getFormats() Parameter: This method does not take any argument as a parameter.Return Value: This method returns format used for pr 2 min read MessageFormat parse() method in Java with Example : Set - 2 The parse() method of java.text.MessageFormat class is used to parse the string object starting from the passed parse position in the parse() method.Syntax: public Object[] parse(String source, ParsePosition pos) Parameter: This method takes the following arguments as parameter. source :- string ove 2 min read MessageFormat setFormat() method in Java with Example The setFormats() method of java.text.MessageFormat class is used to set the new format element at the particular index in the pattern of this message format object. Syntax: public void setFormat(int formatElementIndex, Format newFormat) Parameters: This method takes the following argument as a param 2 min read MessageFormat parse() method in Java with Example : Set - 1 The parse() method of java.text.MessageFormat class is used to parse the string object from the beginning of the index.Syntax: public Object[] parse(String source) throws ParseException Parameter: This method takes string source as an argument over which parsing is going to take place.Return Value: 2 min read Like