Is something wrong with my code?


#1

import java.lang.;
import java.util.
;

public class Main {
public static void main(String[] args) {
// YOUR CODE GOES HERE
// Please take input and print output to standard input/output (stdin/stdout)
// DO NOT USE ARGUMENTS FOR INPUTS
// E.g. ‘Scanner’ for input & ‘System.out’ for output

    Scanner s = new Scanner(System.in);
    
    int a = s.nextInt();
    
    ArrayList arr= new ArrayList<>();
    while(a>=0)
    {
        arr.add(a);
        a=s.nextInt();
    }
    
    s.close();
    
    for(int i=arr.size()-1;i>=0;i--)
    {
        System.out.print(" "+arr.get(i));
    }
    return;
}

}

on test its right on submission its wrong, doesnt show what my mistake is , works good on other IDE’s for the given test cases.


#2

Try switching

       System.out.print(" "+arr.get(i));

to

       System.out.print(arr.get(i) + " ");

and maybe add Integer to arraylist initialization
ArrayList Integer arr= new ArrayList();

Note it does not allow to write triangle brackets in this comment editor but add them around integer of course


#3

ArrayList ar = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int i=0;
ar.add(sc.nextInt());
while(ar.get(i)>0)
{
i++;
ar.add(sc.nextInt());
}
int p=ar.size();
for(int j=p-2;j>=0;j–)
{
System.out.print(ar.get(j)+ " ");
}
:your code corrected :slight_smile: