How can we take multiple inputs in java ? Can we do this "Scanner a,b,c = new Scanner(System.in); | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

How can we take multiple inputs in java ? Can we do this "Scanner a,b,c = new Scanner(System.in);

And also can we use a,b,c without assigning it to another variables and using that variables Instead of a,b,c ?

27th Aug 2019, 8:12 AM
Zeeshan Ahmad
Zeeshan Ahmad - avatar
5 Antworten
+ 4
//Create the Scanner object Scanner s = new Scanner(System.in); //Declare some string variables String a,b,c; //when the input screen pops up and you enter charactors on 3 diffrenet lines each line is assigned to a seprate variable a=s.nextLine(); //line 1 b=s.nextLine(); //line 2 c=s.nextLine(); //line 3 System.out.print(a+" "+b+" "+c);
27th Aug 2019, 8:46 AM
D_Stark
D_Stark - avatar
+ 3
You have to make this yourself. When you do 'Scanner a,b,c = new Scanner(System.in)' you have declared 3 variables of type Scanner and initalized the "c" variable with a refrenece to the Scanner object.. a,b are declared to be of type Scanner but not initalized so there not doing anything. I wouldent say its practical to declare variables like this for Scanner as you restrict your self with what you can do with them because you cant store a String inside a Scanner Variable they are of diffrent types.
27th Aug 2019, 10:04 AM
D_Stark
D_Stark - avatar
+ 3
Zeeshan Ahmad I created a work around so that you can declare all your variables on the same line but unless you know about type casting and inheritance It might be difficult to understand. Object a,b,c,s = new Scanner(System.in); a=((Scanner)s).nextLine(); b=((Scanner)s).nextLine(); c=((Scanner)s).nextLine(); System.out.print(((String)a)+" "+((String)b)+" "+((String)c)+" ");
27th Aug 2019, 11:41 AM
D_Stark
D_Stark - avatar
+ 1
D_Stark we have to do this or this happens itself
27th Aug 2019, 9:06 AM
Zeeshan Ahmad
Zeeshan Ahmad - avatar
0
if you create two Scanner input objects from System.in you easy get error, it is not good idea
27th Aug 2019, 11:16 PM
zemiak