ChoiceFormat setChoices() method in Java with Examples

The setChoices() method of java.text.ChoiceFormat class is used to set the new Choice item with a defined limit and format in Choiceformat object.

Syntax:

public void setChoices(double[] limits, String[] formats)

Parameter: This method takes the following parameters:

  • limits: which is the array of double values which will contain limit for choiceitem.
  • format: which is the array of string values which will contain the format for choiceitem.

Return Value: This method does not return anything.

Exception: This method throws NullPointerException if limit or format is null.



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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// setChoices() 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 new double array
            double[] newlimit = { 1, 2, 3 };
  
            // creating and initializing new String array
            String[] newformat = { "sun", "mon", "tue" };
  
            // setting the limit and format in
            // ChoiceFormat Object
            // using setChoices() method
            cf1.setChoices(newlimit, newformat);
  
            // display the result
            System.out.print("updated Choiceformat object: "
                             + cf1.toPattern());
        }
        catch (NullPointerException e) {
            System.out.println("\nLimit or Format is null");
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


Output:

updated Choiceformat object: 1.0#sun|2.0#mon|3.0#tue

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// setChoices() 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 new double array
            double[] newlimit = null;
  
            // creating and initializing new String array
            String[] newformat = { "sun", "mon", "tue" };
  
            // setting the limit and format in
            // ChoiceFormat Object
            // using setChoices() method
            cf1.setChoices(newlimit, newformat);
  
            // display the result
            System.out.print("updated Choiceformat object: "
                             + cf1.toPattern());
        }
        catch (NullPointerException e) {
            System.out.println("Limit or Format is null");
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


Output:

Limit or Format is null
Exception thrown: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ChoiceFormat.html#setChoices-double:A-java.lang.String:A-

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.