Output to Console

Instead of write and writeln in Pascal we often use System.out.print and System.out.println, respectively, in Oxygene for Java. Whereas write and writeln allow us to pass multiple arguments of different types, System.out.print and System.out.println both allow only one argument. If this argument is a string, it can be made up of multiple substrings concatenated using the + operator e.g. System.out.println('Distance: ' + Distance.toString);

You can use System.out.printf to write multiple types. This is also useful for formatting real numbers (Single or Double) in Oxygene for Java. The symbol sequences %d and %f are placeholders for an Integer and Float, respectively. Optionally you can supply after the % symbol the field width and following a decimal point the number of places of decimals. (Compare with writeln(MyReal : 5 : 2) in Pascal). For example, you can use the statement System.out.printf('Distance: %d Time: %6.2f', Distance, Time), where Distance is an Integer and Time is a Float, Single or Double. The escape sequence %n used in the demonstration program represents a new line.

Note: Output of the pound sign £ is surprisingly difficult in Windows according to an excellent article. In a batch file, you can precede the command to translate the Java bytecode with the command chcp 1252 to change the code page. This only works if you select a True-Type font such as Lucida Console. The use of GBP instead of £ to denote pounds sterling is recommended.

You should be able to predict the output of the following demonstration code.

namespace console_output;

interface

type
  ConsoleApp = class    
  public
    class method Main(args : array of String);    
  end;

implementation

class method ConsoleApp.Main(args : array of String);
var  
  MyInt : Integer := 42;
  MySingle : Single := 1.2345;
  MyChar : Char := 'a';
  MyString : String := 'Answer: ';
begin 
  // Output one variable at a time.
  System.out.println(MyChar); 
  System.out.println(MyInt);
  System.out.println(MySingle);
  // Output a string composed of concatenated substrings.
  System.out.println('Integer value: ' + MyInt.toString);
  // Output a string and integer using printf.
  // %n creates a new line.
  System.out.printf(MyString + '%d%n', MyInt);
  // Output a string and formatted float using printf.
  // (Can use console instead of out).
  System.console.printf('Single value to 2 dp:%5.2f%n', MySingle);
  // Output a string containing more than one number. 
  System.out.printf('Integer value: %d%nSingle to 1 dp: %.1f%n', MyInt, MySingle);
  System.in.read;
end;

end.

Equivalent Code in RemObjects C#

using java.util;

namespace console_output_cs_java
{
  static class Program
  {
    public static void Main(string[] args)
    {
      Integer myInt = 42;
      Single mySingle = 1.2345;
      Char myChar = 'a';
      String myString = "Answer ";
      // Output one variable at a time.
      System.@out.println(myChar); 
      System.@out.println(myInt);
      System.@out.println(mySingle);
      // Output a string composed of concatenated substrings.
      System.@out.println("Integer value " + myInt.toString());
      // Output a string and integer using printf.
      // %n creates a new line.
      System.@out.printf(myString + "%d%n", myInt);
      // Output a string and formatted float using printf.
      // (Can use console instead of out).
      System.@out.printf("Single value to 2 dp:%5.2f%n", mySingle);
      // Output a string containing more than one number. 
      System.@out.printf("Integer value: %d%nSingle to 1 dp: %.1f%n",
                          myInt, mySingle);
      System.@in.read();
    }
  }
}
Programming - a skill for life!

Input, output and access to a Firebird database in Oxygene for Java console applications