Unexpected Exception

sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:7)

@Rishabh8488,
Can you please share your complete code?

import java.util.*;
public class Main {
private static Scanner sc;
public static void main(String[] args) {
// TODO Auto-generated method stub
sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
System.out.print(checkSort(arr,0));
}
public static boolean checkSort(int[] arr,int i) {
if(i==arr.length-1) {
return true;
}
if(arr[i]>arr[i+1]) {
return false;
}
boolean ans=checkSort(arr,i+1);
return ans;
}
}

This exception only occurs on the Online Compiler, The code runs perfectly on Eclipse

@Rishabh8488,
your code is correct. There might be a technical issue with the servers. Kindly drop a mail to [email protected]. They will resolve the issue.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.