Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ChoiceFormat format() method in Java with Examples

  • Last Updated : 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);
        }
    }
}
Output: 
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);
        }
    }
}
Output: 
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-
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!