1

I have a problem in showing the decimals on the average. It keeps showing .00 or .1. I tried to put it in double, but I still get the same results.Also, I want to include the first integer that I input to the sum, but I have no idea how.Please help:

import java.io.*;
import java.util.*;

public class WhileSentinelSum 
{
static final int SENTINEL= -999;

public static void main(String[] args)
{
    // Keyboard Initialization
        Scanner kbin = new Scanner(System.in);

    //Variable Initialization and Declaration
        int number, counter;
        int sum =0;
        int average;

    //Input
        System.out.print("Enter an integer number: " );
        number = kbin.nextInt();
        counter=0;


    //Processing
    //Output
        while(number != SENTINEL)
        {
            counter++;
            System.out.print("Enter an  integer number: ");
            number = kbin.nextInt();
            sum += number;
        }

        if (counter !=0)
            System.out.println("\nCounter is: " + counter);
        else
            System.out.println("no input");

        average= sum/counter;
        System.out.println("The sum is " + sum);
        System.out.println("The average is " + average);


        System.out.println();

    }//end main
}//end class
eLg
  • 519
  • 4
  • 11
  • 25
  • `average` must be a `double`. At the assignment of average, do this computation: `average = (sum + 0.0)/counter;` – Justin Apr 03 '13 at 22:07
  • Check out this: http://stackoverflow.com/questions/2909451/java-simple-division-in-java-bug-feature – Justin Apr 03 '13 at 22:11

5 Answers5

5

Even if you declared average to be double, the sum and counter variables are int, so Java still uses integer division before assigning it to average, e.g. 5 / 10 results in 0 instead of 0.5.

Cast one of the variables to double before the division to force a floating point operation:

double average;

...
average = (double) sum / counter;

You can include the first number in your calculation by processing it before you go into the while loop (if it's not the sentinel value).

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Thank you,how to add the first integer that I input to the sum? – eLg Apr 03 '13 at 22:13
  • 1
    The same way you add into the sum in your while loop. – rgettman Apr 03 '13 at 22:14
  • its giving a different answer – eLg Apr 03 '13 at 22:18
  • Different as in incorrect? Make sure you _count_ that first number too. – rgettman Apr 03 '13 at 22:21
  • i did..Enter an integer number: 1 Enter an integer number: 1 Enter an integer number: 1 Enter an integer number: -999 Counter is: 3.0 The sum is -1996.0 The average is -665.3333333333334 – eLg Apr 03 '13 at 22:26
  • Then you'll have to debug your application to ensure that all 3 ones are being counted and summed, and that the sentinel value _isn't_ being counted and summed. – rgettman Apr 03 '13 at 22:29
1
average = sum/counter;

This line is performing integer division (i.e. it cuts off the decimal part of the true result). You should change it to

average = (double) sum / counter;  // perform double division, not int division

which first casts sum as a double and, consequently, performs regular double division.

Note that you will need to change the type of the average variable to double.


Just to illustrate my point:

int a = 3;
int b = 2;

System.out.println(a / b);
System.out.println((double) a / b);
1
1.5
arshajii
  • 127,459
  • 24
  • 238
  • 287
1

It looks like the problem is in the line that says

average = sum/counter;

Even if you make average a double, sum/counter is still integer division which will yield an integer answer. You could try saying

double average = ((double)sum)/counter;

Another solution would be to make sum or counter of type double and also make average a double.

mottese
  • 192
  • 3
  • 13
  • Technically speaking, there is no need to enclose `(double) sum` in parentheses... The cast will be applied to _sum_ regardless. That being said, you can make an argument _in favor of_ the parens by saying it's more expressive. – jahroy Apr 03 '13 at 22:15
0

Initialize sum and average to double. Use kbin.nextDouble().

  • It's probably better to leave them as ints, but to cast one of them to a double for the division. – jahroy Apr 03 '13 at 22:16
0

It is said: Never use double or float, when exact results are needed. You should prefer BigDecimal instead. the primitives don't use a rounding mode most poeple would expect.

Mordechai
  • 15,437
  • 2
  • 41
  • 82