Solved
Help NEEDED. Need some assistance with my Java code.
Posted on 2004-09-30
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