All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object
|
+----java.text.Format
|
+----java.text.NumberFormat
|
+----java.text.ChoiceFormat
X matches j just in case limit[j] <= X < limit[j+1]
If there is no match, then either the first or last index is used, depending
on whether the number is too low or too high. The length of the array of
formats must be the same as the length of the array of limits.
For example,
{1,2,3,4,5,6,7},
{"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}
{0, 1, ChoiceFormat.nextDouble(0)},
{"no files", "one file", "many files"}
(nextDouble can be used to get the next higher double, to make the half-open
interval.)
Here is a simple example that shows formatting and parsing:
double[] limits = {1,2,3,4,5,6,7};
String[] monthNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
ChoiceFormat form = new ChoiceFormat(limits, monthNames);
ParseStatus status = new ParseStatus();
for (double i = 0.0; i <= 8.0; ++i) {
status.startAt = 0;
System.out.println(i + " -> " + form.format(i) + " -> "
+ form.parse(form.format(i),status));
}
Here is a more complex example, with a pattern format.
double[] filelimits = {0,1,2};
String[] filepart = {"are no files","is one file","are %2 files"};
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
Format[] testFormats = {fileform, null, NumberFormat.getDefault()};
MessageFormat pattform
= new MessageFormat("There %0 on %1",testFormats);
Object[] testArgs = {null, "ADisk", null};
for (int i = 0; i < 4; ++i) {
testArgs[0] = new Integer(i);
testArgs[2] = testArgs[0];
System.out.print(pattform.getPattern() + " -> ");
System.out.println(pattform.format(testArgs));
}
public ChoiceFormat(double limits[],
Object formats[])
public void setChoices(double limits[],
Object formats[])
public double[] getLimits()
public Object[] getFormats()
public StringBuffer format(long number,
StringBuffer toAppendTo,
FormatStatus status)
public StringBuffer format(double number,
StringBuffer toAppendTo,
FormatStatus status)
public Number parse(String text,
ParseStatus status)
public final static double nextDouble(double d)
Used to make half-open intervals.
public final static double previousDouble(double d)
public static double nextDouble(double d,
boolean positive)
All Packages Class Hierarchy This Package Previous Next Index