<i18n dev> Scanner and NumberFormatProvider

Yoshito Umaoka y.umaoka at gmail.com
Fri Oct 13 17:39:12 UTC 2017


I received a problem report from an ICU4J consumer who uses ICU4J Locale SPI module.
ICU NumberFormatProvider returns a class - com.ibm.icu.impl.jdkadapter.NumberFormatICU which extends java.text.NumberFormat.

The ICU4J locale SPI user found the ICU provider causes an issue when he uses Scanner. I can reproduce the problem on my environment as well. The exception call stack shows -

Exception in thread "main" java.lang.ClassCastException: com.ibm.icu.impl.jdkadapter.NumberFormatICU cannot be cast to java.text.DecimalFormat
	at java.util.Scanner.useLocale(Scanner.java:1186)
	at java.util.Scanner.<init>(Scanner.java:540)
	at java.util.Scanner.<init>(Scanner.java:563)
	at test.ScannerTest.main(ScannerTest.java:11)

Below is the location in Scanner.java 

    public Scanner useLocale(Locale locale) {
        if (locale.equals(this.locale))
            return this;

        this.locale = locale;
        DecimalFormat df =
        DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);

So the implementation blindly cast NumberFormat instance returned by getNumberIntance(Locale) to DecimalFormat.
I thought I reported this issue before. Are you aware of this issue? Maybe, I forgot to file a bug when I investigated this earlier in this year, because I could not find it in java bug.


More information about the i18n-dev mailing list