While coding a random program about how Convenient the program was for the user , I use Scanner, and its .nextInt(); to ask the user to rate  the program's Convenience:

275.Scanner scan = new Scanner(System.in);
276.System.out.println("Rate the convenience of this program by typing 1 to 5...");
277.System.out.println("1. *");
278.System.out.println("2. * *");
279.System.out.println("3. * * *");
280.System.out.println("4. * * * *");
281.System.out.println("5. * * * * *");
282.int inp;
283.inp = scan.nextInt(); 

I get the error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at expences.Main.main(Main.java:283)

Recommended Answers

All 3 Replies

There's nothing wrong in the code you posted, so the problem lies somewhere else. Without the whole code and the actual input there's nothing more we can do.

But in general the NoSuchElementException is thrown when the input stream has been closed, so cannot supply any more input for scanning. One way to achieve that is to create a new Scanner using System.in, then close the Scanner, then try to create another. Alternatively it may be a problem how you are running the program - eg multiple versions open at the same time?

commented: "Number 5... is alive! Malfunction! Need input!" +15
commented: I haven't closed any of the inputs and it worked! +0

My apologies. Too many irons in the fire today. I didn't read your post carefully enough.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.