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
// Java program to demonstrate applyPattern() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { // creating and initializing limit double [] limit = { 1 , 2 , 3 }; // creating and initializing format String[] format = { "sun" , "mon" , "tue" }; // creating and initializing ChoiceFormat ChoiceFormat cf = new ChoiceFormat(limit, format); // display the result System.out.println( "current pattern : " + cf.toPattern()); // applying the new pattern // using applyPattern() method cf.applyPattern( "4#wed| 5#thu | 6#fri | 7#sat" ); // display the result System.out.println( "\nnew pattern : " + cf.toPattern()); } } |
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
// Java program to demonstrate applyPattern() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // creating and initializing limit double [] limit = { 1 , 2 , 3 }; // creating and initializing format String[] format = { "sun" , "mon" , "tue" }; // creating and initializing ChoiceFormat ChoiceFormat cf = new ChoiceFormat(limit, format); // display the result System.out.println( "current pattern : " + cf.toPattern()); // applying the new pattern // using applyPattern() method cf.applyPattern( null ); // display the result System.out.println( "\nnew pattern : " + cf.toPattern()); } catch (NullPointerException e) { System.out.println( "\nString is Null" ); System.out.println( "Exception thrown : " + e); } } } |
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-