Scanner class error

I AM GETTING THESE SCANNER CLASS ERROR.

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:18)

CODE:
import java.util.Scanner;

public class Main {

static boolean isSorted(int[] arr, int i) {
    if (i == arr.length - 1) {
        return true;
    }
    if (arr[i] > arr[i + 1]) {
        return false;
    }

    return isSorted(arr, i + 1);
}

public static void main(String[] args) {
    Scanner 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();
    }

    boolean ans = isSorted(arr, 0);
    System.out.println(ans);
}

}

Please help me to resolve these error.