-4

I am trying to write a program that will take an integer and add up each of the number’s digits, count the number of digits in the number, and then average these two numbers.

I must use a while loop and % to access each of the individual digits. Use / to reduce the number so that you can average all of the digits.

234 has a digit average of 3.0,

10000 has a digit average of 0.2,

111 has a digit average of 1.0

Here is my code:

import static java.lang.System.*;

        public class DigitMath
        {
           private static int countDigits( int number )
        {
            int count = 0;
            while( number > 0 )
            {
               count++;
               number = number / 10;
            }
            return count;
        }
        private static int sumDigits( int number )
        {
            double total = 0;
            while( number > 0 )
            {
               total = total + number % 10;
               number = number / 10;
            }
            return number;
        }


        public static double averageDigits( int number )
        {
            if( number > 0 )
                  return sumDigits( number ) / countDigits( number );
          return 0;
        }
    }


    // other code

    import static java.lang.System.*;

    public class Lab09e
    {
        public static void main( String args[] )
        {
            out.println(DigitMath.averageDigits(234));
       }
    }
Micheal O'Dwyer
  • 1,193
  • 1
  • 13
  • 25
NightCode
  • 29
  • 5
  • 2
    Welcome to Stack Overflow! Please read [this guide](https://stackoverflow.com/help/how-to-ask) on how to ask a good question and before posting read [this checklist](https://meta.stackoverflow.com/q/260648/8390068). Please [format your code blocks correctly](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) because the indentation is incorrect. – Micheal O'Dwyer Feb 10 '18 at 19:05
  • 2
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Feb 10 '18 at 19:26
  • What do you think will happen when you divide the result of sumDigits(), which is an integer, by the result of countDigits(), which is also also an integer? – FredK Feb 10 '18 at 19:48
  • @gpasch there was no need for that at all. – achAmháin Feb 10 '18 at 20:07
  • @notyou Agreed. – Micheal O'Dwyer Feb 10 '18 at 20:07

2 Answers2

0

It looks like you are starting with programming so a helpful tip is try to debug by printing intermediate results. Check if your sum and count work correctly, this will help you tackle the problem in smaller segments.

In your code, one very obvious mistake is that when you calculate the average you are dividing an int with an int, which will result in an int. For instance, 4/5 will be 0 and not 0.8 as you would want. Read up on this link or refer to this question to understand this better.

You can solve this by first casting the operands to double or multiplying by 1.0.

Hope this helps!

Stuti Rastogi
  • 1,085
  • 1
  • 15
  • 25
0

The issue is that you are returning number in the sumDigits method instead of total.

When you are returning number you are actually returning 0 which then causes the averageDigits to return 0/3 which results in 0.0.

To fix this problem:

  • Return total instead of number, which is 0 at the end of the method's execution.

  • Also, change the return type of the methods countDigits() and sumDigits() to double instead of an int. Because you are calculating averages you should make sure you that the return expression doing the division will return a double. Dividing two ints will return a double, however, if you divide two doubles it will return a double.

Here is the fixed code:

class DigitMath
{
    private static double countDigits( int number )
    {
        double count = 0.0;
        while( number > 0 )
        {
            count++;
            number = number / 10;
        }
        return count;
    }
    private static double sumDigits( int number )
        {
            double total = 0;
            while( number > 0 )
            {
                total = total + number % 10;
                number = number / 10;
            }
            return total;
        }


    public static double averageDigits( int number )
        {
            if( number > 0 ){
                return sumDigits( number ) / countDigits( number );
            }
          return 0.0;
        }
}



public class Lab09e
{
    public static void main( String args[] )
    {
        System.out.println(DigitMath.averageDigits(234));
    }
}

I hope that this answer helped you and if you have any further questions please feel free to leave a comment below.

Micheal O'Dwyer
  • 1,193
  • 1
  • 13
  • 25