Please Help me I am not able To Do It in JAVA

, ,

input

1 2 3 4 5 (we don’t know the size)

7 6

6 3 5 4 2

.

.

.

Output:

10(sum of numbers)

13

27

.
.

.

Here we don’t know the number of test cases

And We also don’t know how many numbers to input for each test case

Can anyone provide a Solution for it in JAVA

I was trying to solve it but not able to solve it

@vijju123 pls help me

same problem with me

You can use getline() to take entire line as input. The code snippet is like-

string s;
	while(getline(cin,s))
	{
	    .......
	}

@vijju123 ok But how to take number of testcases we don’t know the number of testcase

If all the numbers are specified to be in a single line then you can take input while you don’t encounter a newline using getchar() function. This function takes input character by character . We then convert this character to number and append it to our number .

Here I have wriiten a code describing the same .

It will take input only till its available. If no more input is available, it will exit the loop. Execute it yourself and try once.

string s;
while (getline(cin,s))
{
if(s[0]==’\0’)break;
}

modification of@vijju123

You dont need the condition to break, while loop exits automatically :stuck_out_tongue:

It isn’t exiting without condition as it still waits for input

No, I tried executing it on compiler before posting here. It very well exits.

Thanks @vijju123 It’s working correctly

If any one can tell that how to do the same thing in JAVA

read about those hasNext() functions etc.