當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。