public static void Calculate(StacksUsingArrays stack, int q) throws Exception {
//Write Your Code Here
/* Donot initialize another Scanner use the static scanner already declared*/
for(int i =0;i<q;i++){
int a = s.nextInt();
if(a == 2){
int b = s.nextInt();
stack.push(b);
}else if(a==1){
if(stack.isEmpty()){
continue;
}else{
System.out.println(stack.pop());
}
}
}
}
Please see where I went wrong also what will happen if stack is empty and we encounter 1 like first input is 1