Saturday, January 31, 2026
HomeLanguagesJavaChoiceFormat getFormats() method in Java with Examples

ChoiceFormat getFormats() method in Java with Examples

The getFormats() method of java.text.ChoiceFormat class is used to get the attached format to ChoiceFormat object in time of initialization. It provides the array of specified type.
Syntax: 
 

public Object[] getFormats()

Parameter: This method does not accept any parameter.
Return Value: This method returns an array of the specified type which is the format attached to the ChoiceFormat object.
Below are the examples to illustrate the getFormats() method:
Example 1: 
 

Java




// Java program to demonstrate
// getFormats() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // creating and initializing ChoiceFormat
        ChoiceFormat cf1
            = new ChoiceFormat(
                "4#wed| 5#thu | 6#fri | 7#sat");
 
        // getting format attached
        // to the ChoiceFormat
        // using getFormats() method
        String[] format
            = (String[])cf1.getFormats();
 
        // display the result
        System.out.print("Format: "
                         + Arrays.toString(format));
    }
}


Output: 

Format: [wed, thu , fri , sat]

 

Example 2: 
 

Java




// Java program to demonstrate
// getFormats() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // creating and initializing limit
        double[] limit = { 1, 2, 3, 4 };
 
        // creating and initializing format
        String[] format
            = { "add", "sub",
                "multiply", "divide" };
 
        // creating and initializing ChoiceFormat
        ChoiceFormat cf
            = new ChoiceFormat(limit, format);
 
        // getting format attached
        // to the ChoiceFormat
        // using getFormats() method
        String[] format
            = (String[])cf.getFormats();
 
        // display the result
        System.out.print("Format: "
                         + Arrays.toString(format));
    }
}


Output: 

Format: [add, sub, multiply, divide]

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ChoiceFormat.html#getFormats–
 

RELATED ARTICLES

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
123 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS