java.util.NoSuchElementException Error

I was trying to solve the simple Input challenge and i wrote this code: https://ide.codingblocks.com/s/332319

It passes 3 test cases but is not able to pass one test case. I’m not able to understand why. Please Explain.

My code shows same error for
1
2
88
-89
49
while no error for
1
2
88
-100
49

Please Reply as soon as possible

you should break when sum < 0
if(sum < 0) break;
System.out.println(n);

Still getting that error could you please explain it a little Breifly

@mananmodi.0108
I am writing the algorithm below, hope it helps in clarifying the logic.

declare the variables, n, sum = 0.
take input for n,  
add n to the sum, like sum = sum + n
while (sum >= 0 && scn.hasnext) //do this till sum doesnt become negative and has input
{ 
    //print the last value of n that was added to the sum and didnt make the same negative
    cout << n
    //keep taking user input for more values
    scn.nextInt()
    // keep appending it to the sum to update it
    sum  = sum + n
}

corrected code: https://ide.codingblocks.com/s/332493
Please mark your doubt as resolved and rate as well :slight_smile: