ChoiceFormat format() method in Java with Examples - GeeksforGeeks

19 Aug, 2021
The format() method of java.text.ChoiceFormat class is used to get the appended string builder of the format value of particular limit value passed as parameter and text passed as parameter in this method. Syntax:  
public StringBuffer format(double number,
                         StringBuffer toAppendTo,
                         FieldPosition status)
Parameter: This method takes the following parameter as follow  
number: which is the particular limit of choiceformat object for which format have to be found and appended
toAppendTo: which is the new text which is to be appended with the format
status: which determines if there is no special status to be returned
Return Value: This method return appended value of text and format in the form of StringBuffer object. Exception: This method throws NullPointerException if toAppendto value is null.Below are the examples to illustrate the format() method:Example 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);        }    }}
Formated text with appended value: Sunfri 
 
Example 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
 
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ChoiceFormat.html#format-double-java.lang.StringBuffer-java.text.FieldPosition- 
gulshankumarar231
Java- ChoiceFormat
Java-Functions
Java-text package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java