Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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

  • Last Updated : 08 Oct, 2021

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: 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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-




My Personal Notes arrow_drop_up
Recommended Articles
Page :