ChoiceFormat applyPattern() method in Java with Examples
The applyPattern() method of java.text.ChoiceFormat class is used to set the new pattern text for current ChoiceFormat by overriding the current limit and format. This new pattern will be the combination of limit and format of ChoiceFormat
Syntax:
public void applyPattern(String newPattern)
Parameter: This method takes newPattern as parameter which is the new text pattern for ChoiceFormat.
Return Value: This method returns nothing.
Exception: This method throws NullPointerException if the specified newPattern is null.
Below are the examples to illustrate the applyPattern() method:
Example 1:
Java
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
double [] limit = { 1 , 2 , 3 };
String[] format = { "sun" , "mon" , "tue" };
ChoiceFormat cf
= new ChoiceFormat(limit, format);
System.out.println( "current pattern : "
+ cf.toPattern());
cf.applyPattern( "4#wed| 5#thu | 6#fri | 7#sat" );
System.out.println( "\nnew pattern : "
+ cf.toPattern());
}
}
|
Output:
current pattern : 1.0#sun|2.0#mon|3.0#tue
new pattern : 4.0#wed|5.0#thu |6.0#fri |7.0#sat
Example 2:
Java
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
double [] limit = { 1 , 2 , 3 };
String[] format = { "sun" , "mon" , "tue" };
ChoiceFormat cf
= new ChoiceFormat(limit, format);
System.out.println( "current pattern : "
+ cf.toPattern());
cf.applyPattern( null );
System.out.println( "\nnew pattern : "
+ cf.toPattern());
}
catch (NullPointerException e) {
System.out.println( "\nString is Null" );
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
current pattern : 1.0#sun|2.0#mon|3.0#tue
String is Null
Exception thrown : java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ChoiceFormat.html#applyPattern-java.lang.String-
Last Updated :
21 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...