+ 1

Error with the Scanner, unknown source

I am creating a greeting program in Java: import java.util.Scanner; public class greeting { public static void main(String[] args){ System.out.println("What is your first name?"); Scanner one = new Scanner(System.in); String firstname = one.next(); System.out.println("What is your last name?"); Scanner two = new Scanner(System.in); String lastname = two.next(); System.out.println("Hello, " + firstname + " " + lastname + "!"); } } When I run the program in the Java Code Playground, I get the following error message: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at personalgreeting.main(personalgreeting.java:12) What is wrong with the code? Thanks

5th May 2017, 10:58 PM
Kaien Yang
Kaien Yang - avatar
3 Answers
+ 16
That's like it is at the code playground. All inputs are taken at the very beginning. It's not possible to force the program to do outputs before the input popup appears at the code playground. Only exception: Web codes.
5th May 2017, 10:51 PM
Tashi N
Tashi N - avatar
+ 15
Hugh? Did you edit the question or am I lost? Anyways, you can use Scanner's nextLine() method - next() doesn't exist.
5th May 2017, 11:02 PM
Tashi N
Tashi N - avatar
0
And technically there is no reason to create two Scanners. You can use one over and over.
6th May 2017, 12:18 AM
1of3
1of3 - avatar