Scanner throws an error in a for loop

Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)

Code:

    Scanner sc= new Scanner(System.in);
    int input1=sc.nextInt();
    
    int arr[] = new int[input1];
    

    for(int i = 0 ; i < input1 ; i++){
      
        arr[i] =  sc.nextInt();
      
     
    }

Scanner uses space delimited input. You would need to use sc.nextLine(). Check out other input methods in the complete documentation.

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.