Open In App

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

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
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-



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads