0

Java-Can't take two inputs using Scanner

Could someone please check whats wrong with the following code? It throws an exception as follows when i try to run it import java.util.Scanner; class myClass{ public static void main(String[]args){ String name; int age; Scanner in=new Scanner(System.in); System.out.println("What is your name? "); name = in.nextLine(); System.out.println("What is your age? "); age = in.nextInt(); System.out.printf("Hello %s, age %d\n", name, age); } } (What is your name? What is your age? 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 name.main(name.java:23))

2nd Jul 2017, 2:43 PM
SumiandDave Jones
SumiandDave Jones - avatar
12 Answers
+ 19
you can input multiple choices by splitting them into two lines. Like for eg., First input (your name) and on the next line... Second input (your age). /* According to your code, you can't input only your name. Your age should be on the next Line while inputting your name */
2nd Jul 2017, 2:37 PM
Dev
Dev - avatar
+ 19
@SumiandDave : No, that doesn't work here! You cannot pop-up another input window once it's closed. It's a missing feature in Sololearn's code playground.
2nd Jul 2017, 3:00 PM
Dev
Dev - avatar
+ 5
@Manual There's no need for that.
2nd Jul 2017, 2:56 PM
Harsh
Harsh - avatar
+ 4
@Manual Don't confuse him, its eclipse. BTW, Dayve has already answered his query.
2nd Jul 2017, 2:50 PM
Harsh
Harsh - avatar
+ 4
@Sum line 1 input [ press enter ] line 2 input 2
2nd Jul 2017, 3:00 PM
Manual
Manual - avatar
+ 4
@Suminand Dave You will need to alter it to make it accept input in separate lines.
2nd Jul 2017, 3:03 PM
Harsh
Harsh - avatar
+ 3
Dayve, Tried it but it doesn't work.,same exception again..The problem is that it doesn't ask for the second input 'age' System.out.println("What is your name? "); name = in.nextLine(); System.out.printf("Hello %s,", name); System.out.println("What is your age? "); age = in.nextInt(); System.out.printf(" age %d\n",age); (What is your name? Hello david,What is your age? Exception in thread "main" java.util.NoSuchElementException)
2nd Jul 2017, 2:55 PM
SumiandDave Jones
SumiandDave Jones - avatar
+ 3
@Sum Paste my next answer in to the input It should work for your code
2nd Jul 2017, 3:13 PM
Manual
Manual - avatar
+ 3
Manual 89
2nd Jul 2017, 3:13 PM
Manual
Manual - avatar
+ 2
@Harsh Oh I could not see @Dayve's post when I made that, I am deleting the comment
2nd Jul 2017, 2:54 PM
Manual
Manual - avatar
+ 2
@Dayve, mate I just saw your comment regarding the second input window on sololearn. I did what you suggested and it works fine now.Thank you..
2nd Jul 2017, 3:39 PM
SumiandDave Jones
SumiandDave Jones - avatar
+ 1
Hey Guys, I'm running it in the Code playground of solo learn and It looks like it's an environmental issue rather than anything to do with the source code. thanks for trying to help anyway
2nd Jul 2017, 3:25 PM
SumiandDave Jones
SumiandDave Jones - avatar