ChoiceFormat format() method in Java with Examples
Last Updated :
05 Jul, 2022
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
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
ChoiceFormat cf1
= new ChoiceFormat(
"4#wed| 5#thu | 6#fri | 7#sat" );
StringBuffer str = new StringBuffer( "Sun" );
StringBuffer value = cf1.format( 6 , str, null );
System.out.print( "Formatted text with appended value: "
+ value.toString());
}
catch (NullPointerException e) {
System.out.println( "str is null" );
System.out.println( "Exception thrown: " + e);
}
}
}
|
Output
Formatted text with appended value: Sunfri
Example 2:
Java
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
ChoiceFormat cf1
= new ChoiceFormat(
"4#wed| 5#thu | 6#fri | 7#sat" );
StringBuffer str = null ;
StringBuffer value = cf1.format( 6 , str, null );
System.out.print( "Formatted 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-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...