-1

Can you assist me in writing this Java syntax in Pascal?

A[m] = scan.nextInt();

This program will request the length of the array and then the items in it from the user. This code was written by me:

WriteLn('How many Number you would like to sort:');
ReadLn(size);

For m := 1 to size do 
  if m = 1 then 
    Begin 
      WriteLn;
      WriteLn('Input the first value: ');
    End;

I had no idea how to finish it, but I was familiar with the Java syntax.

for(m = 0; m<size; m++)
{
    if(m == 0)
    {   
        System.out.println("");
        System.out.print("^_^ Input The First Value:");
        A[m] = scan.nextInt();
    }
    else if(m == size -1)
    {
        System.out.print("^_^ Input The Last Value:");
        A[m] = scan.nextInt();
    }
    else
    {
        System.out.print("^_^ Input The Next Value:");
        A[m]= scan.nextInt();
    }
}
0

5 Answers 5

2

Try the following.

readln(a[m]);
Sign up to request clarification or add additional context in comments.

Comments

0
read(a[m]);

should work perfectly.

1 Comment

Instead of readln? stackoverflow.com/a/4370964
0

Java’s nextInt ignores leading white space including newlines. While Pascal’s read and readLn also ignore leading white space, only readLn can consume end‐of‐line character sequences. Thus to get the same behavior of nextInt you have to take care of skipping newlines by yourself:

    { Reads and returns the next `integer` on standard `input`. }
    function nextInteger: integer;
        var
            result: integer;
        begin
            while EOLn do
            begin
                { Discard everything up to and including EOL sequence. }
                readLn
            end;
            read(result);
            nextInteger := result
        end;

Note that Pascal’s built‐in procedures read/readLn accept (at least) integer literals exactly as they would be valid in your Pascal source code. This is different than Java’s readInt that also accepts, for example, digit group separators based on the current locale.

Comments

-1

Typing blind here, but hopefully the concept comes across:

{ in declarations }
var A: array[1..99] of integer;   { or however many you need; data workspace }
    m: integer;   { loop index }
    size: integer;


{ in code }
fillchar(A,sizeof(A),#0);  { just good practice to clear out the workspace before starting}
writeln('How many Number you would like to sort:');
readln(size);

For m := 1 to size do
  begin
    if (m=1) then 
      writeln('Input the first value: ')
    else
      if (m=size) then
        writeln('Input the last value: ')
      else
        writeln('Input the next value: ');
    readln(a[m]);
  end;

Note that I've used a 1-indexed array rather than a zero-indexed one. No reason other than I prefer it that way, saves using "-1" all over the place.

Comments

-2

This is an attempt to use an array to receive the data. The array ps have its size limited by a variable.

var / type
A = array [1..13] : integer;
m : integer;

repeat
    readln (A[m]);
    m:=m+1;
until (m<=10);

I think...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.