Multiple Inputs in same line.
This one is for beginners!!
Usually the input format in competitive programming questions is as follows:
2 3 2 1 2
1 2 3
4 5 6 1
. .. so on
Well don't worry here is a solution. I will be discussing a couple of cases which you may face.
Case 1: Method 1
1 2 3 4 5
Java Code:
Note: use BufferedInputStream for reading inputs in JAVA because it is considered as the fastest way of reading inputs. Scanner class
The following code will read five inputs in same line and print the same.
import java.io.*;
class inputMlines{
public static void main(String[] args)throws IOException{
BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
double[] j= new double[5];
String[] s2 = inp.readLine().split(" ");
j[0]=Double.parseDouble(s2[0]);
j[1]=Double.parseDouble(s2[1]);
String[] s2 = inp.readLine().split(" ");
j[0]=Double.parseDouble(s2[0]);
j[1]=Double.parseDouble(s2[1]);
j[2]=Double.parseDouble(s2[2]);
j[3]=Double.parseDouble(s2[3]);
j[4]=Double.parseDouble(s2[4]);
for(int i=0;i<5;i++)
System.out.print(j[i]+" ");
}//main function ends
} // class closed
==================================================================
Case 2: Method 1
1 2 3
2 3 3
Java Code:
the following code takes input in the above format and prints nothing.
import java.io.*;
class inputMlines{
public static void main(String[] args)throws IOException{
BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
double[] j= new double[3];
for(int input=0;input<2;input++){
String[] s2 = inp.readLine().split(" ");
j[0]=Double.parseDouble(s2[0]);
j[1]=Double.parseDouble(s2[1]);
String[] s2 = inp.readLine().split(" ");
j[0]=Double.parseDouble(s2[0]);
j[1]=Double.parseDouble(s2[1]);
j[2]=Double.parseDouble(s2[2]);
}//for loop ends.
}//main function ends
} // class closed
you can generalise this method after trying yourself.
==================================================================
That is all for this post. Please leave your QUERIES, SUGGESTIONS, BETTER METHODS OR any CORRECTIONS you think in the comments.
Have a nice day!!