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.

  • source :- string over which parsing is going to perform.
  • pos :- it is the starting index of parsing.

Return Value: This method returns array of object as an output.

Exception: This method throws NullPointerException if the parse position is null.



Below are the examples to illustrate the parseObject() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// parseObject() 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";
  
            // creating and initializing ParsePosition
            ParsePosition pos = new ParsePosition(0);
  
            // parsing the string starting from pos
            // accoridng to MessageFormat
            // using parseObject() method
            Object[] hash = (Object[])mf.parseObject(str, pos);
  
            // display the result
            System.out.println("Parsed value are :");
            for (int i = 0; i < hash.length; i++)
                System.out.println(hash[i]);
        }
        catch (NullPointerException e) {
            System.out.println("Parse position is Null");
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Parsed value are :
10.456
30.444
20.325

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// parseObject() 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";
  
            // creating and initializing ParsePosition
            // ParsePosition pos = new ParsePosition(0);
  
            // parsing the string starting from pos
            // accoridng to MessageFormat
            // using parseObject() method
            Object[] hash = (Object[])mf.parseObject(str, null);
  
            // display the result
            System.out.println("Parsed value are :");
            for (int i = 0; i < hash.length; i++)
                System.out.println(hash[i]);
        }
        catch (NullPointerException e) {
            System.out.println("Parse position is Null");
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Parse position is Null
Exception thrown : java.lang.NullPointerException

Reference:https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#parseObject-java.lang.String-java.text.ParsePosition-

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




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.