Results 1 to 7 of 7
  1. #1
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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. #3
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    still doesnt end..is there something i should type in to end it after i enter 4 numbers

  4. #4
    klexe is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

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

  5. #5
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

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

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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. #7
    klexe is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    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.

Similar Threads

  1. for loop help
    By soc86 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 09:45 PM
  2. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  3. do while loop
    By sr20guy in forum New To Java
    Replies: 16
    Last Post: 04-08-2010, 03:59 AM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. while loop
    By Unknown1369 in forum New To Java
    Replies: 5
    Last Post: 07-08-2008, 10:15 AM

Posting Permissions

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