0

Why Scanner closed...?

java.lang.IllegalStateException: Scanner closed >ERROR> i got this error and was unable to solve it. Please help me with this. //// 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 StudentsDistribution.MainProgram.TARTUP(MainProgram.java:29) at StudentsDistribution.MainProgram.main(MainProgram.java:103) C:\Users\WinDows\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) //// https://code.sololearn.com/cHZ18h1tR1rL/?ref=app

3rd Oct 2020, 1:35 PM
SH S
SH S - avatar
5 Answers
+ 1
Remove line 58 info.close() and try again. That should probably work.
3rd Oct 2020, 1:55 PM
Avinesh
Avinesh - avatar
+ 1
Make sure that your 'coursesLine' when split into 'courses' has only 3 values because your 'coursesNUM' is 3. Something like this- String coursesLine = "Java C++ Python"; String[] courses = coursesLine.split(" "); for(String c:courses) System.out.println(c); OR remove the variable 'coursesNUM' and inside the for loop you can use courses.length to iterate.
3rd Oct 2020, 3:08 PM
Avinesh
Avinesh - avatar
0
Avinesh thank you, when i remove it other error appeared “ArrayIndexOutOfBoundsException: 1” at line 23
3rd Oct 2020, 2:24 PM
SH S
SH S - avatar
0
Avinesh it dose not work but thank you
3rd Oct 2020, 7:02 PM
SH S
SH S - avatar
0
String coursesLine = info.nextLine(); String[] courses = coursesLine.split(" "); System.out.print("The courses for this semester are:"); for (int i = 0; i < courses.length; i++) { System.out.print(" " + courses[i]); } This can remove array index out of bounds exception.
3rd Oct 2020, 10:04 PM
Jayakrishna 🇼🇳