+ 5

How to like make multiple scanners at once in java

I made this code import java.util.Scanner; public class Program { public static void main (String[] args) { Scanner firstname = new Scanner(System.in); Scanner lastname = new Scanner(System.in); Scanner age = new Scanner(System.in); System.out.println("My name is " + firstname.nextLine() + " " + lastname.nextLine() + " " + "and I am " + age.nextInt() + " years old." ); } } but it doesn't work... help me

28th Jan 2018, 2:12 AM
Rodrigo Sanchez
Rodrigo Sanchez - avatar
5 Réponses
+ 7
you no need to have one Scanner object for every input, you can use just one for all inputs, just read input first and you can put that inputs in some variables and then printing it...
28th Jan 2018, 2:18 AM
Vukan
Vukan - avatar
+ 5
You don't need multiple scanners, like so: https://code.sololearn.com/cQ4A2oJ7PdiN/?ref=app
28th Jan 2018, 4:13 AM
blackcat1111
blackcat1111 - avatar
+ 4
your code is correct as I try but make sure you enter correct inputs.
28th Jan 2018, 2:34 AM
Waseem Siddiqi
Waseem Siddiqi - avatar
+ 1
See some further info here. Scanner isn't really the thing taking data in, but what is allowing you to use the data taken in https://www.sololearn.com/Discuss/1032676/?ref=app
28th Jan 2018, 8:58 AM
Dan Walker
Dan Walker - avatar
+ 1
Thank you guys so much... it was very helpful
28th Jan 2018, 4:17 PM
Rodrigo Sanchez
Rodrigo Sanchez - avatar