How to make the code work when in else case it takes input

I think I should loop it some way so that it again checks the condition starting from if loop but I do not know how to do it.
https://ide.codingblocks.com/s/137657 Here is my code

Hi Tanjul,
instead of using while(sc.hasNext()) you can use while(flag==1). Initialize a flag variable as 1 initially and if input is ‘x’ or ‘X’ put flag=0. Also remove the input you are taking after printing Invalid operation. This approach should work. If it doesn’t work, feel free to ping me.

https://ide.codingblocks.com/s/138022 I used the while loop while(flag==1) and in the c=‘x’ or c=‘X’ I did flag =0 as you said sir,but in that case I still got 3 test cases wrong . But in the above code I removed flag=0 condition and it works perfectly. Why is it so sir. I my opinion it should have gone in an infinite loop

Can you also share with me the code you wrote with flag=0 when c=‘x’ or ‘X’?

https://ide.codingblocks.com/s/138427 This is the code with flag=0.

https://ide.codingblocks.com/s/138427 this is the code with flag=0.Sorry the above link was invalid Sir

Oh no Tanjul, there’s a slight mistake. In your code:
else if(c==‘x’ || c==‘X’)
System.exit(0);
else
{
System.out.println(“Invalid operation. Try again.”);
flag=0;
}
you’ve put the condition in the wrong place. I wanted you to put flag=0 under the condition if c=‘x’ or ‘X’. If you replace flag=0 with system.exit(0) and remove it from the else block, this code will work. Here we’ve used the flag condition as the terminating condition. If operation is invalid, we can try again with another input but in the above code snippet it will terminate the loop.