Scanner not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Scanner not working

I did exactly what I did just like other coding websites. import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String first; String last; System.out.println("Enter your first name"); first = input.nextLine(); System.out.println("Enter your last name"); last = input.nextLine(); System.out.print(first + last); } } The error says Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at Main.main(Main.java:17)

10th Nov 2020, 5:15 PM
Kevin Yao
Kevin Yao - avatar
4 Answers
+ 7
There‘s nothing wrong with your code. But if you don‘t submit anything, it will throw an error because there is no line to read from. That‘s exactly what the error message is saying. Here in SoloLearn there is no interactivity except for web based projects. So you have to enter your input before you run the code.
10th Nov 2020, 5:39 PM
_cm_
+ 2
On "your program needs input" pop-up, input : ex: Kevin Yao Kevin (press enter). Yao (press submit). You have to enter all needed inputs before running code here..
11th Nov 2020, 3:25 PM
Jayakrishna 🇮🇳
+ 1
Just use next()method instead of nextLine(). Your code is works fine in another app May be sololearn compiler is something different 😊 https://code.sololearn.com/c6nUm6b2qj94/?ref=app
10th Nov 2020, 5:29 PM
NavyaSri
NavyaSri - avatar
+ 1
_cm_ thanks your information I am still confused now it clear.
10th Nov 2020, 5:46 PM
NavyaSri
NavyaSri - avatar