M
metaperl
I've been a programmer for many years and am just learning Java. In
the code below, I think that the for loop from
j=0 to j=3 is poor code. Instead there should be an array of
"pointers" to each getInstance function and one should iterate through
that "dereferencing" each function pointer.
Pseudo code -
FunctionPointer fplist[] = { getInstance, getIntegerInstance,
getCurrencyInstance, getPercentInstance } ;
for (fp : fplist) {
fp->(locales)
}
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
class nf {
public static void main(String[] arg) {
// Print out a number using the localized number, integer, currency,
// and percent format for each locale
Locale[] locales = NumberFormat.getAvailableLocales();
double myNumber = -1234.56;
NumberFormat form;
for (int j=0; j<4; ++j) {
System.out.println("FORMAT");
for (int i = 0; i < locales.length; ++i) {
if (locales.getCountry().length() == 0) {
continue; // Skip language-only locales
}
System.out.print(locales.getDisplayName());
switch (j) {
case 0:
form = NumberFormat.getInstance(locales); break;
case 1:
form = NumberFormat.getIntegerInstance(locales); break;
case 2:
form = NumberFormat.getCurrencyInstance(locales); break;
default:
form = NumberFormat.getPercentInstance(locales); break;
}
if (form instanceof DecimalFormat) {
System.out.print(": " + ((DecimalFormat) form).toPattern());
}
System.out.print(" -> " + form.format(myNumber));
try {
System.out.println(" -> " + form.parse(form.format(myNumber)));
} catch (ParseException e) {}
}
}
}
}
the code below, I think that the for loop from
j=0 to j=3 is poor code. Instead there should be an array of
"pointers" to each getInstance function and one should iterate through
that "dereferencing" each function pointer.
Pseudo code -
FunctionPointer fplist[] = { getInstance, getIntegerInstance,
getCurrencyInstance, getPercentInstance } ;
for (fp : fplist) {
fp->(locales)
}
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
class nf {
public static void main(String[] arg) {
// Print out a number using the localized number, integer, currency,
// and percent format for each locale
Locale[] locales = NumberFormat.getAvailableLocales();
double myNumber = -1234.56;
NumberFormat form;
for (int j=0; j<4; ++j) {
System.out.println("FORMAT");
for (int i = 0; i < locales.length; ++i) {
if (locales.getCountry().length() == 0) {
continue; // Skip language-only locales
}
System.out.print(locales.getDisplayName());
switch (j) {
case 0:
form = NumberFormat.getInstance(locales); break;
case 1:
form = NumberFormat.getIntegerInstance(locales); break;
case 2:
form = NumberFormat.getCurrencyInstance(locales); break;
default:
form = NumberFormat.getPercentInstance(locales); break;
}
if (form instanceof DecimalFormat) {
System.out.print(": " + ((DecimalFormat) form).toPattern());
}
System.out.print(" -> " + form.format(myNumber));
try {
System.out.println(" -> " + form.parse(form.format(myNumber)));
} catch (ParseException e) {}
}
}
}
}