I have this output :
[1, 2, 3, 4, 5, 6, 7, 8] Enter a number : 4 3 Enter a number : Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at NumberElementsSubArray.main(NumberElementsSubArray.java:36)
This is a portion of my code where I think the problem comes from :
while (true) { try (Scanner input = new Scanner(System.in)) { System.out.print("Enter a number : "); int userInput = input.nextInt(); if (userInput == 0) break; if (userInput != 0) System.out.println(getNumberElementSubArray(userInput, numbers)); // print the number of elements // of // subArray } }
Can you explain me this error? And how to fix it?
It is weird because earlier after a lot of test case I didn't have any issue & it did run for more than 2 user input. Now this comes up right after one user input.