# help using do while loop

• 10-29-2010, 11:14 PM
clemsontigers
help using do while loop
having trouble using do while loop

this is what im supposed to do:

Write a program that will read an unspecified number of positive numbers from the keyboard and determine the sum and the average of these numbers. A negative number should be used to terminate the input, i.e. when the program encounters a negative number, it should stop prompting and reading data and at that point should output the results. Note that the negative number that terminates the looping should not be added to the sum and certainly should not be counted as one of the valid numbers.
A sample program run would look like this :
Enter a number that is positive or zero 3
Enter a number that is positive or zero 5
Enter a number that is positive or zero 6
Enter a number that is positive or zero 4
Enter a number that is positive or zero -1
You have entered 4 numbers
Total of the numbers you entered is 18
Average of the numbers is 4.5

The program should use a do WHILE loop to accomplish it's mission.

here is my code and every time i run the program the output "enter a number that is positive or zero" doesnt end

import java.util.Scanner ;

public class Assign7_2_Roberts{

public static void main (String [] args){

int data;
int number = 0, sum = 0, numamount = 0; // You can change the variable names.
//Create Scanner
Scanner input = new Scanner(System.in);

do { // Begin do-while statement
System.out.println("Enter a number that is positive or zero: "); // Get the next number
data = input.nextInt();

sum += data; // Get the sum so far.
} while (data != 0); // End while statement.

double average = sum/numamount; //Get the average.
System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers
System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers
System.out.println("Average of the numbers is " + average); // Output average of numbers.
}
}
• 10-29-2010, 11:43 PM
Fubarable
You need to get and deal with the end of line token for the scanner to move to the next line:

Code:

```    do {         System.out.println("Enter  a  number  that  is  positive  or  zero: ");         data = input.nextInt();         input.nextLine();  // **** here ****         sum += data; // Get the sum so far.     } while (data != 0); // End while statement.```
• 10-30-2010, 12:31 AM
clemsontigers
still doesnt end..is there something i should type in to end it after i enter 4 numbers
• 10-30-2010, 01:56 AM
klexe
First you didnt update your counter (numamount) it set to 0 in your prgram, and to answer the question you have to add if statement to check if the value is equal or greater than 0 otherwise quit without doing any calculation.
Code:

```public class Assign7_2_Roberts {   public static void main (String [] args)   {   int data;   int number = 0, sum = 0, numamount=0; // You can change the variable names.   //Create Scanner   Scanner input = new Scanner(System.in);   do { // Begin do-while statement       System.out.println("Enter a number that is positive or zero(negative to terminate): "); // Get the next number       data = input.nextInt();       if(data>=0){    // You can delete this if statement, but this is required in your question           numamount++; // You had to add this counter or your numamount           sum += data; // Get the sum so far.         }       } while (data >= 0); // End while statement when negative number entered .   double average = sum/numamount; //Get the average.   System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers   System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers   System.out.println("Average of the numbers is " + average); // Output average of numbers.   } }```
• 10-30-2010, 02:14 AM
clemsontigers
thanks man that helped a lot..now im having to do the same thing except using the for loop and its saying the local variable data may not be initialized

Code:

```public class Assign7_1_Roberts{                 public static void main (String [] args){                 int data;                 int i = 0, number = 0, sum = 0, numamount = 0; // You can change the variable names.                 //Create Scanner                 Scanner input = new Scanner(System.in);                                                                         for (i = 0; i <1000; i++) // Begin for-while statement                 {                 System.out.println("Enter  a  number  that  is  positive  or  zero: "); // Get the next number                 data = input.nextInt();                 if(data>=0){    // You can delete this if statement, but this is required in your question                   numamount++; // You had to add this counter or your numamount                   sum += data; // Get the sum so far.                 }                 }while (data >= 0); // End while statement.                                 double average = sum/numamount;  //Get the average.                 System.out.println("You have entered " + numamount + " numbers"); // Output amount of numbers                 System.out.println("Total of the numbers you entered is " + sum); // Output sum of numbers                 System.out.println("Average of the numbers is " + average); // Output average of numbers.                 } }```
• 10-30-2010, 02:45 AM
al_Marshy_1981
initialise the variable data to zero and you should be fine
• 10-30-2010, 03:01 AM
klexe
Easy just break the loop when number less than zero and delete the while statement
Code:

```//after the if statment   else{           break;     }```
I am not sure why you set the counter to 1000, if you want to change that then declear a new var and assign to 1 then ++ that

Code:

```int counter=1;     for(int i=0; i<counter; i++){       //input here       if(input>=0){       //calc here             counter++;           }```

but when the user enters negative in the first loop the program will terminate with error, to solve that add if/else statement after the for loop and tell the program what to do beofre displaying the output. You should use the var(numamount) in the if/else statement.