Any of those help Do a sc.hasNextLine() check before scanning the next line to make sure the next line exists. You don't need scanner declaration twice.
Why is this code giving NoSuchElementException?
By : ASHWIN.A.P.
Date : March 29 2020, 07:55 AM
Any of those help I'm using eclipse. I can't seem to wrap my head around why this code is giving an error. It seems completely logical, and the second I remove that little line, the program works fine. I'm pretty sure the focus should be on line 98, that is what the error is saying as well. The other part I made into a comment also gives the exact same error for seemingly no reason... why would it be going too far in the objectList? Line 98 looks like this , It seems completely logical ...
Any of those help NoSuchElementException is thrown, because you call input.close() extensively, where in your case it should be called only once. I have removed input.close(); occurrences (except main method) and your code worked fine. There reason for this that calling Scanner.close() will in fact close whole System.in and every future related method calls, like input.nextInt() in your case, will result in Exception.