Text ChoiceFormat in Java With Example

0
28
Spread the love
  • 23
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    132
    Shares

"<yoastmark

This post will cover ChoiceFormat in java,ChoiceFormat java in details,ChoiceFormat java example,ChoiceFormat java apis,ChoiceFormat java class structure.

ChoiceFormat in java

ChoiceFormat is a subclass of Format.It converts a number to the string in the same way on how java switch case works.Every ChoiceFormat has an array of doubles. They presents its limits and an array of Strings. They represents format. When format() method acts to format a number y, the representation will be

limits[j]<=y<limits[j+1]

if y is less than the first element of the given array,then the first element is used. In case y is greater than the last the last element is taken and used. once j value can be determined,the indexed String is returned from format() method.[j is used as the index of the array of String].

Another example:

 X matches j if and only if limit[j] ≤ X < limit[j+1]

A ChoiceFormat object can be created by taking care of “limit” and “format” into a single string. This single string is known as pattern.

A ChoiceFormat enables us to attach a format to a range of numbers. It is generally used in a MessageFormat for handling plurals(many cases). The choice is specified with an ascending list of doubles, where each item specifies a half-open interval up to the next item.

Example of pattern

ChoiceFormat myChoiceFormat=new ChoiceFormat("0#cases|1#case|2#cases");

A choice object is created from cases. When it formats the number 0 or any number greater than or equal to 2,it returns “case” when it formats the number 1.

In this scenario # symbol is used to separate the limit number from String and | symbol is used to separate the different cases.

For better idea on formatting please check SimpleDateFormat and MessageFormat

ChoiceFormat java class structure

public class java.text.ChoiceFormat extends java.text.NumberFormat {
// Public Constructors
public ChoiceFormat(String newPattern);
public ChoiceFormat(double[] limits, String[] formats);
// Class Methods
public static final double nextDouble(double d);
public static double nextDouble(double d, boolean positive);
public static final double previousDouble(double d);
// Public Instance Methods
public void applyPattern(String newPattern);
public Object clone(); // Overrides NumberFormat
public boolean equals(Object obj); // Overrides NumberFormat
public StringBuffer format(long number, StringBuffer toAppendTo,FieldPosition status);
// Defines NumberFormat
public StringBuffer format(double number, StringBuffer toAppendTo,FieldPosition status);
// Defines NumberFormat
public Object[] getFormats();
public double[] getLimits();
public int hashCode(); // Overrides NumberFormat
public Number parse(String text, ParsePosition status);
// Defines NumberFormat
public void setChoices(double[] limits, String[] formats);
public String toPattern();
}

ChoiceFormat java in details

If there is no match fond, then either the first or last index is used, depending on whether the number (X) is too low or too high.

If the limit array is not in ascending order, the results of formatting will be incorrect. ChoiceFormat also accepts \u221E as equivalent to infinity(INF).

Note: ChoiceFormat differs from the other Format classes in that you create a ChoiceFormat object with a constructor (not with a getInstance style factory method).

The factory methods aren’t necessary because ChoiceFormat doesn’t require any complex setup for a given locale. In fact, ChoiceFormat doesn’t implement any locale specific behavior

ChoiceFormat java example

 

  • limits = {1,2,3,4,5,6,7}
    formats = {“Sun”,”Mon”,”Tue”,”Wed”,”Thur”,”Fri”,”Sat”}
  • limits = {0, 1, ChoiceFormat.nextDouble(1)}
    formats = {“no files”, “one file”, “many files”}
    (nextDouble can be used to get the next higher double, to make the half-open interval.)
 double[] limits = {1,2,3,4,5,6,7};
 String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
 ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
 ParsePosition status = new ParsePosition(0);
 for (double i = 0.0; i <= 8.0; ++i) {
     status.setIndex(0);
     System.out.println(i + " -> " + form.format(i) + " -> "
                              + form.parse(form.format(i),status));
 }

important ChoiceFormat java apis

public void applyPattern(String newPattern);

We can change the pattern by using the applyPattern() method if we wish to change.

public String toPattern();

We can get the pattern information by applying toPattern() method in the form of String.

Reference: Oracle ChoiceFormat

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 23
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    132
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here