F2 string format not working in java

Yesterday I installed Elements 11.0.0.2731 and noticed that ‘F2’ string format ceased to work in Java.

Code example:

writeLn(String.Format('{0,9:F2}', 1.0));

Please see the attached mini project.
TestFormatF2.zip (420.4 KB)

I get this error with .2729 Water (Win 11):

An exception occurred in Test, thread 0001 ()

Type: java.lang.ClassCastException

Message:
java.lang.Double cannot be cast to java.lang.Integer

Call Stack:

0000000000000000 __Global.ObjectToInt()
0000000000000000 StringFormatter.NumberValueToString()
0000000000000000 StringFormatter.ProcessStandardNumericFormat()
0000000000000000 StringFormatter.ProcessFormat()
0000000000000000 StringFormatter.FormatString() [repeated]
0000000000000000 String.Format()
0000000000000000 Program.main$() (Program.pas, line 15)
0000000000000000 Program.main() (Program.pas, line 0)

I believe the language being used in your case is Oxygene (.pas file).

namespace Test;

uses
  java.util,
  RemObjects.Elements.RTL;

type
  Program = class
  public

    class method Main(args: array of String): Int32;
    begin
      writeLn(String.Format('{0,9}', 1.0));
      writeLn(String.Format('{0,9}', 1));
      writeLn(String.Format('{0,9:F2}', 1.0));  // Error
      readLn;
    end;

  end;

end.

I started a new Java project (.java) and copied the main code snippet into main and it appears to compile correctly:

package JavaNetCore1a;

public class Program
{
    public static int Main(String []args)
    {
        writeLn(String.Format("{0,9}", 1.0));
        writeLn(String.Format("{0,9}", 1));
        writeLn(String.Format("{0,9:F2}", 1.0));  
        readLn();  // <== need parentheses here for java
        return 0;
    }
}

It returns:

        1
        1
     1.00

Thanks. Yes, exactly: Oxygene, Windows. Tested with Elements 11.0.0.2731, resulting in:

> java -jar test.jar
      1.0
        1
Exception in thread "main" java.lang.ClassCastException: class java.lang.Double cannot be cast to class java.lang.Integer (java.lang.Double and java.lang.Integer are in module java.base of loader 'bootstrap')
        at RemObjects.Elements.RTL.__Global.ObjectToInt()
        at RemObjects.Elements.RTL.StringFormatter.NumberValueToString()
        at RemObjects.Elements.RTL.StringFormatter.ProcessStandardNumericFormat()
        at RemObjects.Elements.RTL.StringFormatter.ProcessFormat()
        at RemObjects.Elements.RTL.StringFormatter.FormatString()
        at RemObjects.Elements.RTL.StringFormatter.FormatString()
        at RemObjects.Elements.RTL.String.Format()
        at Test.Program.main$(D:\RemObjects\Test\Program.pas:15)
        at Test.Program.main(D:\RemObjects\Test\Program.pas)

Let me add that this did work in my previous installation of Elements, which was 11.0.0.2605.

Reproduced. this one might be a compiler regression. Will log.

1 Like

Logged as bugs://E25837.

workaround: rebuild elements.jar lcoally and add this line:

    class method ObjectToInt(aArg: Object): Int64;
    begin
      var lType := typeOf(aArg);
      writeLn($"lType {lType}");
      case lType of
        Int64, UInt64: exit Int64(aArg);
        Integer, UInt32, Byte, SByte, Int16, UInt16: exit Integer(aArg);
        java.lang.Double: exit Integer(Double(aArg)); // <<<< add this line as workaround.
        else exit Integer(aArg);
      end;
    end;
1 Like

bugs://E25837 was closed as fixed.

1 Like