Phil
Phil

Reputation: 1453

Finding the Average from a list of numbers in Java

This is my first attempt at java problem I have been given as part of my Programming assignment.

I must make a program which calculates the average of a list of numbers that a user enters. the data enty should be terminated when 0 is entered. my problem is with this "ALL NEGATIVE NUMBERS SHOULD BE IGNORED"

for some reason the following code does not work, when I enter a negative number it should be ignored but for some reason it terminates the data enty

import java.util.*;

class Task_8
  {
public static void main()
  {

    Scanner inputLine = new Scanner(System.in);

    float row, numberentered, numbersum = 0, negativenumber = 0;
    double result, count = 0;


    System.out.println ("Welcome to Task 8 of 10 of my Programming Assignment... Nearly There!");
    System.out.println ("_____________________________________________________________________");
    System.out.println ();
    System.out.println ("Enter as many numbers as you like and this program will tell you the arithmatic mean");
    System.out.println ("Terminate data entry by entering 0");


   do{

       System.out.print ("Please enter a number: ");
        numberentered = inputLine.nextInt();

        count++;

        if (numberentered < 0)
        {
            numberentered  = negativenumber;   
        }

        numbersum = ( numberentered + numbersum ) - negativenumber;

    }

  while  ( numberentered !=0 );

     result = numbersum/count;
     System.out.println ();
     System.out.println ("*************************************");
     System.out.println ();
     System.out.println ("The sum of all of the numbers you entered is " +numbersum);
     System.out.println ("You entered " + count + " numbers");
     System.out.println ("The Average/mean of the numbers that you entered is " + result);


    System.out.println ();
    System.out.println ("*************************************");

  }
}

any Ideas guys?

Thank you

Upvotes: 3

Views: 4076

Answers (4)

jonmorgan
jonmorgan

Reputation: 2610

Examine the following block of code:

if (numberentered < 0)
{
    numberentered  = negativenumber;   
}

What this is doing is setting numberentered to 0. Then, at the end of your do-while loop, you have this:

while(numberentered != 0);

So, whenever the user types in a negative number, you set numberentered to zero. When you reach the end of the loop, numberentered != 0 is false, so the loop exits.

The simpler solution, and something more akin to what you will learn to do on a regular basis in the future, is to simply check the value of the number in an if statement and then add it or not based on its value.

if(numberentered > 0)
    numbersum += numberentered;

This is clean, concise, and removes the need for the negativenumber variable which is superfluous and could be confusing. If you were looking at this code a year from now, would you remember what negativenumber meant, or why it was set to zero? Write your code as if somebody else will have to read it and understand it. Your professors will, and, in the future, your colleagues will.

On another note, you are reading in integers (with inputLine.nextInt()) but storing them in a float. You've also declared count as a double. You most likely will want to declare count, numberentered, and numbersum as an int.

Upvotes: 2

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81734

The variable negativenumber always has the value zero. When you set numberentered to negativenumber the "while" condition is met, and the loop exits. A better strategy would be to us "continue" to skip the rest of the loop body when a negative number was entered.

Upvotes: 7

dmcnelis
dmcnelis

Reputation: 2923

If you want to ignore the negative number, then don't include it in your calculation. Instead do something like:

if (numberentered > 0)
{
   numbersum += numberentered;
}

Upvotes: 5

esaj
esaj

Reputation: 16035

You never assign a value other than 0 (in initialization) to negativenumber, then you do

if (numberentered < 0)
{
    numberentered  = negativenumber;   
}

and the do...while-loop terminates because numberentered is 0.

Upvotes: 1

Related Questions