Results 1 to 7 of 7
Thread: help using do while loop
 10292010, 11:14 PM #1Member
 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 dowhile 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.
}
}

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.
 10302010, 12:31 AM #3Member
 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
 10302010, 01:56 AM #4Member
 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.
look at this and read the comments I added
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 dowhile 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. } }
 10302010, 02:14 AM #5Member
 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 forwhile 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. } }
 10302010, 02:45 AM #6Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
initialise the variable data to zero and you should be fine
Last edited by al_Marshy_1981; 10302010 at 02:53 AM. Reason: mouth full a variables
 10302010, 03:01 AM #7Member
 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; }
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.
Similar Threads

for loop help
By soc86 in forum New To JavaReplies: 9Last Post: 01242011, 10:45 PM 
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04302010, 05:05 PM 
do while loop
By sr20guy in forum New To JavaReplies: 16Last Post: 04082010, 03:59 AM 
whileloop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06262009, 08:46 PM 
while loop
By Unknown1369 in forum New To JavaReplies: 5Last Post: 07082008, 10:15 AM
Bookmarks