How to take input from user in the following format in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to take input from user in the following format in java?

👉🏻Input Format :-  First line will be the number of testcases, T. Next line will have T integers, denoted by N. Sample Input :- 2 3 15 Like over here in the first line we have 2 which is the value of T(no. of testcases) and in the next line we have T number of integers and since over here T = 2 therefore we are having 2 integers 3 and 15. So, basically I want to ask that how do we take multiple inputs from user in a single line separated by space in Java using Scanner class ? I'm a complete beginner in competitive programming and was trying to solve a problem from hackerrank where they usually ask for this type of input from the user. I googled it but could not find something productive. So, can anyone help me in understanding this input format?

26th Aug 2020, 5:53 PM
Smriti Kaur
Smriti Kaur - avatar
16 Answers
+ 3
Smriti Kaur Not sure whether this will meet the Competitive programming input time limits. Or this link might help. https://www.google.com/url?sa=t&source=web&rct=j&url=https://www.tutorialspoint.com/how-to-input-multiple-values-from-user-in-one-line-in-java&ved=2ahUKEwjczbDuvLnrAhWJXSsKHdnYAc4QFjANegQICBAB&usg=AOvVaw3ixSM1ZmYSKXYB2VNCMwvn import java.util.*; public class Program { public static void main(String[] args) { int t = 0; Scanner in= new Scanner(System.in); t = in.nextInt(); int[] n = new int[t]; for(int i = 0; i < t; i++) n[i] = in.nextInt(); System.out.println(t); for(int i =0; i<n.length;i++ ) System.out.print(n[i]+" "); } } /* copy paste one of this as input(input format) 2 1 2 3 1 2 3 9 1 2 3 4 5 6 7 8 9 */
26th Aug 2020, 6:04 PM
Rohit Kh
Rohit Kh - avatar
+ 2
For reading an Integer in java,. Scanner inp = new Scanner(System.in); now using scanner inject 'inp', you can read all data types... For integer, inp.nextInt(); it will take a next, int value from input stream (whether it is in next line or after a space). nexrLine(); read the entire line as a string from the next position of input. Ex: int a = inp.nextInt(); int b = inp.nextInt(); For this, you can input as 2 3 Or as 2 3 int a = inp.nextInt(); String st = inp.nextLine(); For these, if input is 2 3 4, then a =2, st = "3 4"; if input is 2 3 4 then a =2 and st =""; you last input "3 4". So by nextInt() you can take multiple input by space separating in a single line.. By next(); also for single word of input..... (edited)
26th Aug 2020, 6:29 PM
Jayakrishna 🇮🇳
+ 2
So, that means the following snippet of code can be used for taking input from the user according to the input format i.e., 2 in the first line two space separated integers 3 15 in the second line?⬇️ Scanner sc = new Scanner(System.in) int T = sc.nextInt( ); for(int i =1;i<=T;i++) { int N = sc.nextInt( ); }
27th Aug 2020, 6:22 AM
Smriti Kaur
Smriti Kaur - avatar
+ 1
You can take the first input and store it in variable T then by using Scanner class's object you can take multiple inputs inside a loop that will depend on the value of T. And for your question about taking input in single line, you can go through the question attached below. Thank you https://stackoverflow.com/questions/23506429/how-to-read-multiple-integer-values-from-a-single-line-of-input-in-java
26th Aug 2020, 6:13 PM
Hardik Sharma
Hardik Sharma - avatar
+ 1
a user has to respects this format too. he writes number eg 5 and presses enter then he writes some numbers and spaces, in this example 5 times because the first number was 5 1 2 3 40 50 the program scans it by methods of Scanner: int t = scanner.nextInt(); // for first number while(t-- > 0) System.out.println( scanner.nextInt() ); // for print other numbers instead of print, usually you need store numbers to an array or variables
26th Aug 2020, 7:22 PM
zemiak
+ 1
Jayakrishna🇮🇳 but what is "System inp = new System (System.in); "? I mean u have wriiten it intentionally or by mistake u have typed "System" instead of "Scanner"?
27th Aug 2020, 6:14 AM
Smriti Kaur
Smriti Kaur - avatar
+ 1
Smriti Kaur yes. sry for that. It's typo mistake.. So correct one is Scanner inp = new Scanner(System.in); Yes. That code works for reading.. [Take initialization out to retain N value..]
27th Aug 2020, 10:17 AM
Jayakrishna 🇮🇳
+ 1
zemiak oh okay but then where should I scann EOL to avoid this , after inputting T ?
27th Aug 2020, 10:29 AM
Smriti Kaur
Smriti Kaur - avatar
+ 1
Jayakrishna🇮🇳 oh okay thanks and there's no need for sorry...
27th Aug 2020, 10:29 AM
Smriti Kaur
Smriti Kaur - avatar
+ 1
Thank you all for helping me out...🎉 Jayakrishna🇮🇳 zemiak RKK Hardik Sharma
27th Aug 2020, 10:35 AM
Smriti Kaur
Smriti Kaur - avatar
+ 1
Kaur I checked it, and it works ok .., sometimes it is problem, but not here
27th Aug 2020, 11:13 AM
zemiak
+ 1
Ok so from all this discussion can I conclude that the following code will only use "two" and not "three" lines of input in the input file that is generated in the Hackerrank platform ? Scanner sc = new Scanner(System.in) int T = sc.nextInt( ); for(int i =1;i<=T;i++) { int N = sc.nextInt( ); }
27th Aug 2020, 12:07 PM
Smriti Kaur
Smriti Kaur - avatar
+ 1
Jayakrishna🇮🇳 yes u r absolutely right about the code playground but I want the answer in reference to the IDE used by Hackerrank or hackerearth...that whether the aforementioned code snippet will use 2 lines of input or 3 lines of input ? In the question which they have asked they want the user to use only two lines of input first one contains the value of T and the second line will have T numbers of space separated integers. So, altogether it want 3 inputs from the user (if the value of T is 2) but we are allowed to use only 2 lines of input and not 3 ? I hope now my question is clear?😅
27th Aug 2020, 12:47 PM
Smriti Kaur
Smriti Kaur - avatar
+ 1
Smriti Kaur I think, That description means, just first value representing number of values to be given should be taken to T, and next are values.. So if T =2, you can take input on i=1,2 values into N.. If T=3, you can take, input into N, for i=1,2,3,.. So code snippet works like, Accepting T number of values into N.. Since all intigers , number of lines no effect. If input contains a line of string values, there may comes problems... And is it getting wrong there anything? Did you checked it?
27th Aug 2020, 1:03 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 no I haven't checked it yet... but I think now my doubt is clear. Actually, I got confused in understanding the input format.
27th Aug 2020, 1:24 PM
Smriti Kaur
Smriti Kaur - avatar
0
Smriti Kaur actually in that code snippet, number of input lines doesn't matter.. Check it in code playground by taking input in differet ways..
27th Aug 2020, 12:13 PM
Jayakrishna 🇮🇳