Java Reference
In-Depth Information
// ChoiceFormat to give English text version, quantified
static
static
ChoiceFormat quantizedFormat
=
new
new
ChoiceFormat
(
"0#no reviews|1#one review|1<many reviews"
);
// Test data
static
static
int
int
[]
data
= { -
1
,
0
,
1
,
2
,
3
};
public
public static
void
main
(
String
[]
argv
) {
System
.
out
.
println
(
"Pluralized Format"
);
for
static
void
for
(
int
int
i
:
data
) {
System
.
out
.
println
(
"Found "
+
i
+
" "
+
pluralizedFormat
.
format
(
i
));
}
System
.
out
.
println
(
"Quantized Format"
);
for
for
(
int
int
i
:
data
) {
System
.
out
.
println
(
"Found "
+
quantizedFormat
.
format
(
i
));
}
}
}
This generates the same output as the basic version. It is slightly longer, but more general,
and lends itself better to internationalization.
See Also
In addition to
ChoiceFormat
, the same result can be achieved with a
MessageFormat
, dis-
cussed in
Chapter 15
.
Generating Random Numbers
Problem
You need to generate random numbers in a hurry.