当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ChoiceFormat setChoices()用法及代码示例


java.text.ChoiceFormat类的setChoices()方法用于在Choiceformat对象中设置具有定义的限制和格式的新Choice项目。

用法:

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

参数:此方法采用以下参数:


  • limits:这是双精度值数组,其中将包含选择项的限制。
  • format:这是字符串值的数组,其中将包含choiceitem的格式。

返回值:此方法不返回任何内容。

异常:如果limit或format为null,则此方法引发NullPointerException。

下面是说明setChoices()方法的示例:

示例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); 
        } 
    } 
}
输出:
updated Choiceformat object: 1.0#sun|2.0#mon|3.0#tue

示例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); 
        } 
    } 
}
输出:
Limit or Format is null
Exception thrown: java.lang.NullPointerException

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



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ChoiceFormat setChoices() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。