ChoiceFormat format() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 vlaue = cf1.format(6, str, null);
  
            // display the result
            System.out.print("Formated text with appended value: "
                             + vlaue.toString());
        }
        catch (NullPointerException e) {
            System.out.println("str is null");
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


Output:

Formated text with appended value: Sunfri 

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 vlaue = cf1.format(6, str, null);
  
            // display the result
            System.out.print("Formated text with appended value: "
                             + vlaue.toString());
        }
        catch (NullPointerException e) {
            System.out.println("str is null");
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


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-

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.