MessageFormat parse() method in Java with Example : Set - 1 Last Updated : 08 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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: This method returns array of object as an output.Exception: This method throws ParseException if the beginning of the specified string can not be parsed.Below are the examples to illustrate the parse() method:Example 1: Java // Java program to demonstrate // parse() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // creating and initializing MessageFormat MessageFormat mf = new MessageFormat("{0, number, #}, {2, number, #.#}, {1, number, #.##}"); ; // creating and initializing String source String str = "10.456, 20.325, 30.444"; // parsing the string // according to MessageFormat // using parse() method Object[] hash = mf.parse(str); // display the result System.out.println("Parsed value are :"); for (int i = 0; i < hash.length; i++) System.out.println(hash[i]); } catch (ParseException e) { System.out.println("\nString is Null"); System.out.println("Exception thrown : " + e); } } } Output: Parsed value are : 10.456 30.444 20.325 Example 2: Java // Java program to demonstrate // parse() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // creating and initializing MessageFormat MessageFormat mf = new MessageFormat("{0, date}, {2, time}, {1, number}"); // creating and initializing String source String str = "10, 20, 30"; // parsing the string // according to MessageFormat // using parse() method Object[] hash = mf.parse(str); // display the result System.out.println("Parsed value are :"); for (int i = 0; i < hash.length; i++) System.out.println(hash[i]); } catch (ParseException e) { System.out.println("String is Null"); System.out.println("Exception thrown : " + e); } } } Output: String is Null Exception thrown : java.text.ParseException: MessageFormat parse error! Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#parse-java.lang.String- Comment More infoAdvertise with us Next Article MessageFormat parse() method in Java with Example : Set - 1 R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-text package Java-MessageFormat Practice Tags : Java Similar Reads 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 parseObject() method in Java with Example The parseObject() method of java.text.MessageFormat class is used to parse the string object starting from the passed parse position in the parseObject() method.Syntax: public Object parseObject(String source, ParsePosition pos) Parameter: This method takes the following arguments as parameter. sour 2 min read 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 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 toPattern() method in Java with Example The toPattern() method of java.text.MessageFormat class is used to get string representation of the current pattern of this message format object.Syntax: public String toPattern() Parameter: This method does not take any argument as a parameter.Return Value: This method returns string representation 2 min read Like