Link to home
Start Free TrialLog in
Avatar of biloonline
biloonline

asked on

Help NEEDED. Need some assistance with my Java code.

I am asked a write a method that computes the sum of the digits in an integer and I am asked to use the following methhod declaration:

public staticint SumDigits(long n)

For example, sumDigits(123) returns 1+2+3 = 9

Here 's what I've gotten so far:

<code>
import java.io.*;

class Hw3Pr4 {
      public static int sumDigits(long n) throws IOException {
            BufferedReader stdin  = new BufferedReader (new InputStreamReader(System.in));
            
            int number, sum = 0;
            System.out.println("Enter an integer: ");
            
            String numberString = stdin.readLine();
            number = Integer.parseInt(numberString);
            // Sum the digits entered
            sum += number % 10;
        number /= 10;
        sum += number % 10;
        sum += number / 10;
       
        System.out.println("Sum of all digits: " + sum);
      }
}

</code>

I did not know whether to include the public static void main(String[] args) at the very first.

Please advise, am I on the right track?

Thanks
Avatar of biloonline
biloonline

ASKER

I guess I have mis-used the declaration I was given.
Avatar of Mick Barry
you should be calculating the sun of the digits in 'n' shouldn't you (instead of prompting the user for a number)?

you'll need a main in the class you are using to test it.

as far as summing the digits you need to loop thru each digit, the code you have only deals with two digits.
another approach would be to convert the number to a string.
I guess I know how to do it, I 'll give it a shot and see how it goes.

Thanks objects for youe assistance.
<code>


class SumDigits      {
      public static int sumDigits(long n) {

      return 0;
      }



int sum = 0;


      for (int i = digt.length(); i > 0; i--) {

            sum += Character.digit(digt.charAt(i - 1),10);

            digt = digt.substring(0,i - 1);
            
            return sum;
            
            }


public static void main(String [] args) {
      
      System.out.println("SumDigits of " + args[0] + " = " +
      SumDigits.sumDigits(Long.parseLong(args[0])));


      }
}

</code>

Am I missing something?

I am getting 2 errors:

E:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw3Pr1\SumDigits.java:13: illegal start of type
        for(int i = digt.length(); i > 0; i--) {
        ^
E:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw3Pr1\SumDigits.java:29: <identifier> expected
        }
         ^
2 errors

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
your code (that is "for" loop) is outside the method. put it within the method sumDigits().
i'd suggest using the approach mentioned by objects above.
Something like

class Test
{
      public static void main(String[] args)
      {
            new Test().sumDigits(args[0]);
      }
      public void sumDigits(String s)
      {
        int sum = 0;

          for(int i=0; i<s.length(); i++)
            {
           
                  sum = sum + Character.getNumericValue(s.charAt(i));
            }
            System.out.println(sum);
      }
}