Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4228682

MessageFormat format(Object, StringBuffer, FieldPosition) doesn't work

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P3 P3
    • None
    • 1.1.7, 1.3.0, 1.4.0, 1.4.1
    • core-libs
    • None
    • x86, sparc
    • solaris_2.6, windows_nt, windows_2000

      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

            joconnersunw John Oconner (Inactive)
            mcolburnsunw Michael Colburn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: