Input from Keyboard

Keyboard input is achieved with the System.in.read method, which takes an array of SByte as its argument. You can convert this easily to a String; one of the constructors of a string takes an array of SByte as an argument followed by the index of the first SByte to convert then the length of the String. System.in.read returns the number of characters read, including the end of line marker(s). You do not usually wish to include these in your String. The expression System.lineSeparator.length evaluates to 2 for Windows operating systems and 1 for Linux.

namespace keyboard_input1;

interface

uses
  java.util;

implementation

var
  InputBytes: array[0 .. 255] of SByte;
  MyString : String;
  InputLength : Integer;
begin
  InputLength := System.in.read(InputBytes);
  writeln(InputLength);
  MyString := new String(InputBytes, 0, InputLength - System.lineSeparator.length);
  writeln(MyString);
  System.in.read;
end.

You can use the above method to obtain a string to convert to a number using the valueOf method of an Integer or Float. You may find it more convenient to supply a single argument to the String constructor then trim the String down to size, as in the following example. You need to be careful, however; the array of SByte is not automatically initialised upon reuse. Here we use a separate array for each input. In program Roulette, we initialise the array before each input.
namespace keyboard_input2;

interface

uses
  java.util;

implementation

var
  MyInt : Integer;
  MyDouble : Double;
  MyChar : Char;
  strInt, strDouble: String;  
  IntEntry, DoubleEntry, CharEntry : array[0 .. 20] of SByte;
begin
  writeln('Please enter an integer: ');
  System.in.read(IntEntry);
  strInt := new String(IntEntry);
  MyInt := Integer.valueOf(strInt.trim);
  writeln('Your entry: ' + MyInt.toString);

  writeln('Please enter a Double: ');
  System.in.read(DoubleEntry);
  strDouble := new String(DoubleEntry);
  MyDouble := Float.valueOf(strDouble.trim);
  System.out.printf('Your entry to 3 places of decimals: %.3f%n', MyDouble);

  writeln('Please enter a Char: ');
  System.in.read(CharEntry);
  MyChar := chr(CharEntry[0]);
  writeln('Your entry: ' + MyChar); 
  System.in.read;
end.

For simplicity, the above demonstration code contains no validation. The following program shows an easy way to perform a type check on an Integer.

namespace keyboard_input3;

interface

uses
  java.util;

implementation

var
  MyInt : Integer;  
  strInt : String;  
  CurrentEntry : array[0 .. 20] of SByte;
  Error : Boolean;
begin
  repeat
    Error := False;
    writeln('Please enter an integer: ');
    System.in.read(CurrentEntry);
    strInt := new String(CurrentEntry);
    try
      MyInt := Integer.valueOf(strInt.trim);
    except
      Error := True;      
    end;
  until not Error;
  writeln('Your entry: ' + MyInt.toString);
  System.in.read;  
end.

Multiple Entry

Our final example of input from the keyboard is a conversion of program InputFromKeyboard in our tutorial on Pascal I/O.

namespace multiple_entry;

interface

uses
  java.util;

implementation

method MultipleEntry;
var
  InputNums : array[0 .. 255] of SByte;
  Mean, Sum : Single;
  Int1, Int2, Int3 : Integer; 
  strInt1, strInt2, strInt3 : String;
  Count : Integer;
begin
  strInt1 := '';
  strInt2 := '';
  strInt3 := '';
  writeln('Please enter three integers separated by spaces. ');
  System.in.read(InputNums);

  Count := 0;
  while InputNums[Count] <> 32 do
    begin
      strInt1 := strInt1 + chr(InputNums[Count]);
    inc(Count);
    end;
    inc(Count);
  while InputNums[Count] <> 32 do
    begin
      strInt2 := strInt2 + chr(InputNums[Count]);
      inc(Count);
    end;
    inc(Count);
   while InputNums[Count] <> 13 do
    begin
      strInt3 := strInt3 + chr(InputNums[Count]);
      inc(Count);
    end;
  Int1 := Integer.valueOf(strInt1);
  Int2 := Integer.valueOf(strInt2);
  Int3 := Integer.valueOf(strInt3);
  Mean := ( Int1 + Int2 + Int3) / 3.0;  //3.0 not 3
  writeln;
  System.out.printf('Their mean is %.2f', Mean);
  writeln;
end;

method EnterLines;
const
  MAX_CHARS = 1000;
var
  LineCount, TotalChars : Integer;
  Lines : array[0 .. 9] of array[0 .. 255] of SByte;
  CurrentLine : array[0 .. 255] of SByte;
  LineLengths : array[0 .. 9] of Integer;
  Characters : array[0 .. MAX_CHARS - 1] of Char;
begin
  TotalChars := 0;
  LineCount := -1;
  writeln('Please enter several lines of text (# on a new line then Enter to finish).');
  writeln;
  repeat
    inc(LineCount);
    LineLengths[LineCount] := System.in.read(CurrentLine);
    Lines[LineCount] := CurrentLine;
    for i : Integer := 0 to LineLengths[LineCount] do
      Characters[TotalChars + i] := chr(Lines[LineCount][i]);
    TotalChars := TotalChars + LineLengths[LineCount];
  until Lines[LineCount][0] = ord('#');  
  writeln;
  writeln('ASCII values recorded (with each new line represented by'); 
  writeln('13 and 10 (Windows) or 10 (Linux): ');
	
  for Count : Integer := 0 to TotalChars - 1 do
    begin
      write(ord(Characters[Count]).toString + ' ');
    end;
  writeln;
end;

begin
  MultipleEntry;
  EnterLines;
  System.in.read;
end.

Equivalent Code of keyboard_input1 in RemObjects C#

using java.util;

namespace keyboard_input1_cs_java
{
  static class Program
  {
    public static void Main(string[] args)
    {
      SByte[] inputBytes = new SByte[255];
      String myString;
      Integer inputLength;
      inputLength = System.@in.read(inputBytes);
      System.@out.println(inputLength);
      myString = new String(inputBytes, 0, inputLength -
                             System.lineSeparator().length());
      System.@out.println(myString);
      System.@in.read();
    }
  }
}

Equivalent Code of keyboard_input2 in RemObjects C#

using java.util;

namespace keyboard_input2_cs_java
{
  static class Program
  {
    public static void Main(string[] args)
    {
      Integer myInt;
      Double myDouble;
      Char myChar;
      String strInt, strDouble;
      //Initialise arrays to prevent compiler warnings
      SByte[] intEntry = new SByte[12]{0,0,0,0,0,0,0,0,0,0,0,0};
      SByte[] doubleEntry = new SByte[20]
                               {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
      SByte[] charEntry = new SByte[3]{0,0,0};
      System.@out.println("Please enter an integer: ");
      System.@in.read(intEntry);
      strInt = new String(intEntry);
      myInt = Integer.valueOf(strInt.trim());
      System.@out.println("Your entry: " + myInt.toString());
      System.@out.println("Please enter a Double: ");
      System.@in.read(doubleEntry);
      strDouble = new String(doubleEntry);
      myDouble = Float.valueOf(strDouble.trim());
      System.@out.printf("Your entry to 3 places of decimals: %.3f%n",
                          myDouble);
      System.@out.println("Please enter a Char: ");
      System.@in.read(charEntry);
      myChar = chr(charEntry[0]);
      System.@out.println("Your entry: " + myChar); 
      System.@in.read();
    }
  }
}

Equivalent Code of keyboard_input3 in RemObjects C#

using java.util;

namespace keyboard_input3_cs_java
{
  static class Program
  {
    public static void Main(string[] args)
    {
      Integer myInt;
      String strInt;
      SByte[] currentEntry = new SByte[20];
      Boolean error = true;
      while (error)
      {
        error = false;
        System.@out.println("Please enter an integer: ");
        System.@in.read(currentEntry);
        strInt = new String(currentEntry);
        try
        {
          myInt = Integer.valueOf(strInt.trim());
        }
        catch(Exception E)
        {
          error = true;
        }
      }
      System.@out.println("Your entry: " + myInt.toString());
      System.@in.read(); 
    }
  } 
}

Equivalent Code of multiple_entry in RemObjects C#

using java.util;

namespace multiple_entry_cs_java
{
  static class Program
  {
    public void MultipleEntry()
    {
      SByte[] inputNums = new SByte[255];
      Single mean;
      Integer int1, int2, int3; 
      String strInt1, strInt2, strInt3;
      Integer count = 0;
      strInt1 = "";
      strInt2 = "";
      strInt3 = "";
      System.@out.println("Please enter three integers " + 
                          "separated by spaces.");
      System.@in.read(inputNums);

      while (inputNums[count] != 32)
      {
        strInt1 = strInt1 + chr(inputNums[count]);
        inc(count);
      }
      inc(count);
      while (inputNums[count] != 32)
      {
        strInt2 = strInt2 + chr(inputNums[count]);
        inc(count);
      }
      inc(count);
      while (inputNums[count] != 13)
      {
        strInt3 = strInt3 + chr(inputNums[count]);
        inc(count);
      }
      int1 = Integer.valueOf(strInt1);
      int2 = Integer.valueOf(strInt2);
      int3 = Integer.valueOf(strInt3);
      mean = (int1 + int2 + int3) / 3.0;  //3.0 not 3
      System.@out.println();
      System.@out.printf("Their mean is %.2f", mean);
      System.@out.println();
    }

    public void Enterlines()
    {
      const Integer MAX_CHARS = 1000;
      Integer lineCount, totalChars;
      SByte[][] lines = new SByte[9][];
      for (int i = 0; i < 9; i++)
      {
        lines[i] = new SByte[255];
      }
      SByte[] currentLine = new SByte[255];
      Integer[] lineLengths = new Integer[9];
      Char[] characters = new Char[MAX_CHARS - 1] ;

      totalChars = 0;
      lineCount = 0;
      System.@out.println("Please enter several lines of text (# on " +
                          "a new line then Enter to finish).");
      System.@out.println();
      while (lines[lineCount][0] != 35) //while first char in line not #
      {
        if (lineCount > 0)
          inc(lineCount);
        lineLengths[lineCount] = System.@in.read(currentLine);
        lines[lineCount] = currentLine;
        for (Integer i = 0 ; i <= lineLengths[lineCount]; i++)
        {
          characters[totalChars + i] = chr(lines[lineCount][i]);
        }
        totalChars += lineLengths[lineCount];
      }
      System.@out.println();
      System.@out.println("ASCII values recorded (with each new line " +
                          "represented by 13 & 10 (Win) or 10 (Linux)"); 
      System.@out.println();
    
      for (Integer i = 0; i < totalChars; i++)
      {
        write(ord(characters[i]).toString() + " ");
      }
      System.@out.println();
    }

    public static void Main(String[] args)
    {
      MultipleEntry();
      Enterlines();
      System.@in.read();
    }
  }
}
Programming - a skill for life!

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