Read input Until Negative Number is Entered

Loops are usually prepared to run a certain number of times. One way to ensure that the user can enter as many numbers as he/she wants each time the program runs is to establish a loop that continues to receive input until a negative number is written.

Read input Until Negative Number is Entered

Example 1: Create a while loop and set user input as loop condition. We can also find the sum of the numbers entered by the user..

Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a positive number. Or enter a negative number to exit the program");
int input = scanner.nextInt();
int sum = 0;

while(input >= 0)
{
    sum += input;
    System.out.println("Please enter a positive number. Or enter a negative number to exit the program");
    input=scanner.nextInt();
}
System.out.println("Sum of the numbers you entered = " + sum);

Console Output:

Please enter a positive number. Or enter a negative number to exit the program
4
Please enter a positive number. Or enter a negative number to exit the program
116
Please enter a positive number. Or enter a negative number to exit the program
25
Please enter a positive number. Or enter a negative number to exit the program
0
Please enter a positive number. Or enter a negative number to exit the program
99
Please enter a positive number. Or enter a negative number to exit the program
-1
Sum of the numbers you entered = 244
 

Example 2: Let's prepare the same example with for loop. Since the second parameter of the for loop is the loop consition, we can use the for loop in a different way

Scanner scanner = new Scanner(System.in);
int sum = 0;
int input=0;

for (int i=0; input>=0; i++)
{
	System.out.println("Please enter a positive number. Or enter a negative number to exit the program");
    input=scanner.nextInt();
	sum += input;
}
System.out.println("Sum of the numbers you entered = " + sum);

Console Output:

Please enter a positive number. Or enter a negative number to exit the program
4
Please enter a positive number. Or enter a negative number to exit the program
5
Please enter a positive number. Or enter a negative number to exit the program
9
Please enter a positive number. Or enter a negative number to exit the program
116
Please enter a positive number. Or enter a negative number to exit the program
0
Please enter a positive number. Or enter a negative number to exit the program
44
Please enter a positive number. Or enter a negative number to exit the program
-1
Sum of the numbers you entered = 177

 

Example 3: Let's solve the same example with for loop and break statement.

Scanner scanner = new Scanner(System.in);
int sum = 0;
int input=0;

for (int i=0; i >= 0; i++) //endless loop
{
	System.out.println("Please enter a positive number. Or enter a negative number to exit the program");
    input=scanner.nextInt();
	
    if (input<0)
    	break;
    
    sum += input;
}
System.out.println("Sum of the numbers you entered = " + sum);

Console Output:

Please enter a positive number. Or enter a negative number to exit the program
44
Please enter a positive number. Or enter a negative number to exit the program
511
Please enter a positive number. Or enter a negative number to exit the program
0
Please enter a positive number. Or enter a negative number to exit the program
999
Please enter a positive number. Or enter a negative number to exit the program
-1
Sum of the numbers you entered = 1554