ChoiceFormat parse() method in Java with Examples

The parse() method of java.text.ChoiceFormat class is used to get the limit value for particular format in ChoiceFormat object.

Syntax:

public Number parse(String text, ParsePosition status)

Parameter: This method takes the following parameters:

  • text: which is the text for which limit value have to be found in String format.
  • status: which is the index at the which that choice item is present for which limit value to be found.

Return Value: This method returns an array of the specified type which is the format attached to the ChoiceFormat object.

Exception: This method throws NullPointerException if string text or status is null.



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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getFormats() method
  
import java.text.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // creating and initializing ChoiceFormat
            ChoiceFormat cf1
                = new ChoiceFormat(
                    "4#wed| 5#thu | 6#fri | 7#sat");
  
            // creating and initializing ParsePosition
            ParsePosition par = new ParsePosition(0);
  
            // getting limit of particular format
            // of ChoiceFormat Object
            // using getFormats() method
            Number limit
                = cf1.parse("wed", par);
  
            // display the result
            System.out.print("limit: "
                             + limit.intValue());
        }
        catch (NullPointerException e) {
            System.out.println("\nString is Null");
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


Output:

limit: 4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// getFormats() method
  
import java.text.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // creating and initializing ChoiceFormat
            ChoiceFormat cf1
                = new ChoiceFormat(
                    "4#wed| 5#thu | 6#fri | 7#sat");
  
            // creating and initializing ParsePosition
            ParsePosition par = new ParsePosition(0);
  
            // getting limit of particular format
            // of ChoiceFormat Object
            // using getFormats() method
            Number limit
                = cf1.parse(null, par);
  
            // display the result
            System.out.print("limit: "
                             + limit.intValue());
        }
        catch (NullPointerException e) {
            System.out.println("String is Null");
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


Output:

String is Null
Exception thrown: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ChoiceFormat.html#parse-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.