+ 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 ?
5 Answers
+ 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);
+ 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.
+ 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)+" ");
+ 1
D_Stark we have to do this or this happens itself
0
if you create two Scanner input objects from System.in you easy get error, it is not good idea