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)

Paramter: 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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-



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.