java.text.MessageFormat
format(Object single, StringBuffer result, FieldPosition ignore) doesn't
work, get the following exception thrown when program is run:
Exception in thread "main" java.lang.ClassCastException: java.lang.Double
at java.text.MessageFormat.format(Compiled Code)
The problem is with the 1st param Object single. See suggested fix.
The other 3 forms of this method work OK.
Test program: Testit.java
/*
* Test for MessageFormat.format
*
* public final StringBuffer format(Object source,
* StringBuffer result, FieldPosition ignore)
*
* Formats message pattern to produce a string.
*
*/
import java.util.*;
import java.io.*;
import java.lang.*;
import java.text.*;
class Testit
{
public static void main(String arg[])
{
String sExpect = "The disk C contains 45 file(s).";
MessageFormat mf = new MessageFormat("The disk C contains {0} file(s).");
StringBuffer sb1 = new StringBuffer("");
Object obj = new Double(45);
StringBuffer sb2 = mf.format(obj, sb1, null);
System.out.println(sb2);
if ( sb2.toString().equals(sExpect) ) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
}
}
Output from run:
Exception in thread "main" java.lang.ClassCastException: java.lang.Double
at java.text.MessageFormat.format(Compiled Code)
at Testit.main(Compiled Code)
Output with applied fix:
The disk C contains 45 file(s).
Test Passed
format(Object single, StringBuffer result, FieldPosition ignore) doesn't
work, get the following exception thrown when program is run:
Exception in thread "main" java.lang.ClassCastException: java.lang.Double
at java.text.MessageFormat.format(Compiled Code)
The problem is with the 1st param Object single. See suggested fix.
The other 3 forms of this method work OK.
Test program: Testit.java
/*
* Test for MessageFormat.format
*
* public final StringBuffer format(Object source,
* StringBuffer result, FieldPosition ignore)
*
* Formats message pattern to produce a string.
*
*/
import java.util.*;
import java.io.*;
import java.lang.*;
import java.text.*;
class Testit
{
public static void main(String arg[])
{
String sExpect = "The disk C contains 45 file(s).";
MessageFormat mf = new MessageFormat("The disk C contains {0} file(s).");
StringBuffer sb1 = new StringBuffer("");
Object obj = new Double(45);
StringBuffer sb2 = mf.format(obj, sb1, null);
System.out.println(sb2);
if ( sb2.toString().equals(sExpect) ) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
}
}
Output from run:
Exception in thread "main" java.lang.ClassCastException: java.lang.Double
at java.text.MessageFormat.format(Compiled Code)
at Testit.main(Compiled Code)
Output with applied fix:
The disk C contains 45 file(s).
Test Passed
- duplicates
-
JDK-4744262 MessageFormat.format() throws ClassCastException
-
- Closed
-
-
JDK-4407453 MessageFormat.format() Requires Object[] Parameter
-
- Closed
-
-
JDK-8200216 Unchecked cast of arguments for MessageFormat
-
- Closed
-
- relates to
-
JDK-4422080 Applet program does not work properly with appletvier
-
- Closed
-