Taking multiple inputs in single line separated by space in java?
I want to take input in the following way:
Sample Input 1
3
45 3 14
Sample Input 2
5
12 34 5 56 7
The first line is the number of test cases and the second line is the corresponding values for the test cases.
Please give me the java code for taking multiple inputs in the single line separated by a space.
Re: Take multiple inputs in a single line.
We're not a homework service so please don't take the mickey.
Work on it yourself and come back with code and a specific problem you're facing. The simple answer however is to use the Scanner class with the String.split() method to get the inputs.
Re: Take multiple inputs in a single line.
Are you reading from standard input or from a file?
Re: Take multiple inputs in a single line.
Quote:
Originally Posted by
mia_tech
Are you reading from standard input or from a file?
No the input is not read from a file. It is being given by the user in the same way i shown above.
Re: Take multiple inputs in a single line.
If you need help finishing the project post the code you have so far and describe the problem you have.
If you don't know where to start, any search engine is a good place. Look for scanner and string classes for starters.
Re: Take multiple inputs in a single line.
Quote:
Originally Posted by
kunalgaurav18
No the input is not read from a file. It is being given by the user in the same way i shown above.
since your input is in the form of
you could read the first line and then loop through the second line...
Code :
/**code removed by moderator*/
Re: Take multiple inputs in a single line.