# Thread: help using do while loop

1. Member
Join Date
Oct 2010
Posts
44
Rep Power
0

## 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.
}
}

2. You need to get and deal with the end of line token for the scanner to move to the next line:

Java 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.```

3. Member
Join Date
Oct 2010
Posts
44
Rep Power
0
still doesnt end..is there something i should type in to end it after i enter 4 numbers

4. Member
Join Date
Oct 2010
Posts
6
Rep Power
0
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.
Java 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
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.
}
}```

5. Member
Join Date
Oct 2010
Posts
44
Rep Power
0
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

Java 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
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.
}
}```

6. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
8
initialise the variable data to zero and you should be fine
Last edited by al_Marshy_1981; 10-30-2010 at 02:53 AM. Reason: mouth full a variables

7. Member
Join Date
Oct 2010
Posts
6
Rep Power
0
Easy just break the loop when number less than zero and delete the while statement
Java 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

Java 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•