ChoiceFormat setChoices() method in Java with Examples
Last Updated :
16 Dec, 2019
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:
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
ChoiceFormat cf1
= new ChoiceFormat(
"4#wed| 5#thu | 6#fri | 7#sat" );
double [] newlimit = { 1 , 2 , 3 };
String[] newformat = { "sun" , "mon" , "tue" };
cf1.setChoices(newlimit, newformat);
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:
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
ChoiceFormat cf1
= new ChoiceFormat(
"4#wed| 5#thu | 6#fri | 7#sat" );
double [] newlimit = null ;
String[] newformat = { "sun" , "mon" , "tue" };
cf1.setChoices(newlimit, newformat);
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
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ChoiceFormat.html#setChoices-double:A-java.lang.String:A-
Share your thoughts in the comments
Please Login to comment...