1

Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n)

Programming problem 5.2. Page 212.

Please forgive my newness to programming. I'm having a hard time understanding and answering this question. Here's what I have so far. Please assist and if you dont mind, explain what I'm doing wrong.

import java.util.Scanner;

public class PP52v2 {

    public static void main(String [] args) {

    int sum = sumDigits(n);
    System.out.println("The sum is: " + sum);

    }//main

        public static int sumDigits(long n) {

            Scanner input = new Scanner(System.in);

            System.out.println("Enter your digits");
            n = input.nextLong();
            int num = (int)(n);
            int sum;


            while(num > 0) {

            sum += num % 10; //must mod - gives individual numbers
            num = num / 10; //must divide - gives new num

            }//loop
            return sum;
        }//sumDigits

}//class
ylun.ca
  • 2,504
  • 7
  • 26
  • 47
Pivo
  • 31
  • 1
  • 10
  • 2
    You have to explain what you expect to happen and what actually happens. – Sotirios Delimanolis Mar 17 '14 at 00:20
  • You have to pass a number to that method, so get the user input from `main` method and pass it `sumDigits()` – Abimaran Kugathasan Mar 17 '14 at 00:26
  • Thanks for replying. I'm trying to write a method that computes the sum of the digits in an integer. The method is where I'm having trouble. I'm supposed to use the following method header: public static int sumDigits(long n). My problem might be because I'm not casting the values correctly. – Pivo Mar 17 '14 at 00:26

6 Answers6

2

Basically, you should not be handling the user input inside of the method. You should be passing the user input into your method. Other than that, everything looks good. I've made that slight change below:

import java.util.Scanner;

public class PP52v2 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter your digits");
        long n = input.nextLong();
        int sum = sumDigits(n);
        System.out.println("The sum is: " + sum);
    }// main

    public static int sumDigits(long n) {

        int num = (int) (n);
        int sum = 0;

        while (num > 0) {

            sum += num % 10; // must mod - gives individual numbers
            num = num / 10; // must divide - gives new num

        }// loop
        return sum;
    }// sumDigits

}// class
Joel Christophel
  • 2,604
  • 4
  • 30
  • 49
0

Your code works fine for me.

i just changed int sum = sumDigits(n) to int sum = sumDigits(0) since n wasn't declared.

To have it done correctly, you just would have to put your scanner into the main method and pass the result of it (the long value) to your method sumDigits(long n).

glen3b
  • 693
  • 1
  • 8
  • 22
Gee858eeG
  • 185
  • 8
0

Do the prompt

System.out.println("Enter your digits"); n = input.nextLong();

in your main(String[] args) method because n is not currently declared in the scope of the main method.

Anduril_1251
  • 131
  • 4
0
public static int sumDigits(int num) {
    int sum = 0;
    while(num > 0) {

        sum += num % 10; //must mod - gives individual numbers
        num = num / 10; //must divide - gives new number
    } //End loop
    return sum;
}

For one, you should not read in the number within this method, as it accepts the number as a parameter. The method should be invoked after calling long inputNum = input.nextLong(); by using int digitSum = sumDigits((int)inputNum).

glen3b
  • 693
  • 1
  • 8
  • 22
0

When writing a method, you have input, output, and side effects. The goal is to choose the right combination of the three so that the method, and program as a whole, words as expected.

It seems like your method is supposed to take a number as input and return each digit added together into one final sum.

Write A Test

Usually when you program, you come up with some code that uses your imaginary function. This is called a test. For a test, this could work:

System.out.println("123 should be 6: " + sumDigits(123));

Choose A Signature

You've already managed to right the correct signature. Nice!

Implement Method

Here's where you're a bit confused. Read through what every line of code does, and see if it is accomplishing your goal.

// set up a scanner for reading from the command line
// and print a message that you expect digits
Scanner input = new Scanner(System.in);
System.out.println("Enter your digits");

// read the next long number from the input stream
n = input.nextLong();

Why is this part of your method? You already have the number passed in as the argument n.

// cast the number to an integer
int num = (int)(n);

Again, not sure what this is accomplishing, besides the possibility of a bug for large numbers.

// initialize the sum variable to 0.
int sum;

Would be clearer to explicitly set the sum to 0. int sum = 0;

// add the last digit and truncate the number in a loop
while(num > 0) {
    sum += num % 10; //must mod - gives individual numbers
    num = num / 10; //must divide - gives new num
}

// actually return the calculated sum
return sum;

This seems like the only part of the method you need. Hopefully this helps!

AJcodez
  • 31,780
  • 20
  • 84
  • 118
0

Since the input number can be either positive or negative, you need to convert it to its absolute value to get the sum of digits. Then for each iteration, you add the remainder to the total sum until the quotient is 0.

public static int sumDigits(long n) {
    int sum = 0;
    long quotient = Math.abs(n);
    while(quotient > 0) {
        sum += quotient % 10;
        quotient = (long) quotient / 10;
    }
    return sum;
}
tonga
  • 11,749
  • 25
  • 75
  • 96