I’m working on my College project and I keep getting this exception when I try to make all my code loop. When not in a loop it works perfectly fine, it includes creating text files and appending text files (if thats any help). My assumption is that my scanner is causing these problems, but I don’t know what the problem is or how to fix it.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:855)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at PDispenser_V1.main(PDispenser_V1.java:51)
I can’t post my code because plagiarism software checks stackoverflow and my full project will be plagiarised if I do. I know that’s a huge downfall but if you could tell me what to look for in general and how to fix it (provided I gave you enough to go on) that would be great! Also the only line number the complier gave me that exists is 51 and it has this code on it.
selection = input.nextInt();
This line is followed by a switch statement with a bunch of methods in each case. If theres anything I can answer without showing any code please ask. Any help is appreciated.
EDIT: I should also say, it complies fine but when I go to select one of the options again thats when it throws the exception.
EDIT2: I should also mention that the selection is taking a line from the user not a file.
You are probably using nextInt without checking if there is one available to read on the stream, as the javadoc for nextInt says:
Try using hasNextInt() to check if there is an int before attempting to read one from the stream.