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:
// 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); } }} |
Output:
updated Choiceformat object: 1.0#sun|2.0#mon|3.0#tue
Example 2:
// 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); } }} |
Output:
Limit or Format is null Exception thrown: java.lang.NullPointerException



