ChoiceFormat parse() method in Java with Examples

The java.text.ChoiceFormat is a class containing a parse() as a function. To retrieve the limit value for a specific format in a ChoiceFormat object, utilize the ChoiceFormat class.

Syntax:

Parameters:

- where the parameters that this method takes is

text_name: which is the text that has to have its limit value found in String format.

status_Of_choice: which is the index for the limit value to be determined at which that option item is present.

Return Value: The format that is associated with the ChoiceFormat object is returned by this function as an array of the given type.

Exception: If either the status or the string text is null, this function raises a NullPointerException.

Example 1:

The Java program shows how to parse a string to obtain an equivalent numerical limit by using the ChoiceFormat class. A ChoiceFormat object is initialized with a pattern that maps the integers 0 to 5 to the months of January through June. The application tries to parse the string "JANUARY" and obtain the related numeric limit by using a ParsePosition object. If it is successful, the limit-which is 0 for "JANUARY" will be printed.

Implementation:

FileName: ChoiceFormatParseExample1.java

Output:

The limit of the choice element is : 0

Example 2:

The Java program that is provided shows how to handle a NullPointerException when parsing a null string using the ChoiceFormat class. A ChoiceFormat object is initialized by the program with a pattern that maps integers 0 to 5 to the months of January through June. After that, it makes an effort to use a ParsePosition object to parse a null string. The catch block handles the NullPointerException that is thrown when the input string is null. The program shows the details of an issue and releases a message stating that the string is null.

Implementation:

FileName: ChoiceFormatParseExample2.java

Output:

The String is Null
The Exception thrown is : java.lang.NullPointerException: Cannot invoke "String.regionMatches(int, String, int, int)" because "text" is null