📜  Java中的 ChoiceFormat format() 方法及示例

📅  最后修改于: 2022-05-13 01:55:16.664000             🧑  作者: Mango

Java中的 ChoiceFormat format() 方法及示例

Java.text.ChoiceFormat类的format()方法用于获取在该方法中作为参数传递的特定限制值的格式值和作为参数传递的文本的附加字符串构造器。
句法:

public StringBuffer format(double number,
                         StringBuffer toAppendTo,
                         FieldPosition status)

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

  • number:这是必须找到并附加格式的choiceformat对象的特定限制
  • toAppendTo:这是要附加格式的新文本
  • status:判断是否没有特殊状态要返回

返回值:该方法以StringBuffer对象的形式返回文本和格式的附加值
异常:如果 toAppendto 值为 null,则此方法抛出NullPointerException
以下是说明format()方法的示例:
示例 1:

Java
// Java program to demonstrate
// format() 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 StringBuffer
            StringBuffer str = new StringBuffer("Sun");
 
            // getting the required format with appended text
            // ChoiceFormat Object
            // using format() method
            StringBuffer value = cf1.format(6, str, null);
 
            // display the result
            System.out.print("Formated text with appended value: "
                             + value.toString());
        }
        catch (NullPointerException e) {
            System.out.println("str is null");
            System.out.println("Exception thrown: " + e);
        }
    }
}


Java
// Java program to demonstrate
// format() 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 StringBuffer
            StringBuffer str = null;
 
            // getting the required format with appended text
            // ChoiceFormat Object
            // using format() method
            StringBuffer value = cf1.format(6, str, null);
 
            // display the result
            System.out.print("Formated text with appended value: "
                             + value.toString());
        }
        catch (NullPointerException e) {
            System.out.println("str is null");
            System.out.println("Exception thrown: " + e);
        }
    }
}


输出:
Formated text with appended value: Sunfri 

示例 2:

Java

// Java program to demonstrate
// format() 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 StringBuffer
            StringBuffer str = null;
 
            // getting the required format with appended text
            // ChoiceFormat Object
            // using format() method
            StringBuffer value = cf1.format(6, str, null);
 
            // display the result
            System.out.print("Formated text with appended value: "
                             + value.toString());
        }
        catch (NullPointerException e) {
            System.out.println("str is null");
            System.out.println("Exception thrown: " + e);
        }
    }
}
输出:
str is null
Exception thrown: java.lang.NullPointerException

参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/ChoiceFormat.html#format-double-java.lang.StringBuffer-java.text.FieldPosition-