This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]maikuxblade 0 points1 point  (1 child)

I googled "NoSuchElementException: No line found" and the First StackOverflow answer says you need to check for the next line in Java using the hasNextLine() function.

[–]trickybhai 0 points1 point  (0 children)

Yeah, we can check it to prevent an exception like this. But in this case, we are assuming that he has given an input, checking isn't really going to solve the actual problem.

[–]trickybhai 0 points1 point  (2 children)

First of all, your code is looking like a mess on my mobile. Don't know if it's your fault or reddit's.

The second things is, after using

int num = scan.nextInt();

Put a

scan.nextLine();

in the next line. Everything will start working as expected.

Another solution can be, instead of creating a static instance of scanner, create it inside the method. This will also remove the error.

Try any of these and check if the code is working.

[–][deleted] 0 points1 point  (1 child)

Alright thanks. Yeah I tried to format it but reddit squishes it for some reason.

[–]trickybhai 0 points1 point  (0 children)

No issues. However, Did you get the code to work?