aspose file tools*
The moose likes Beginning Java and the fly likes Sum Of Digits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sum Of Digits" Watch "Sum Of Digits" New topic
Author

Sum Of Digits

Warren Oldham
Greenhorn

Joined: Oct 13, 2000
Posts: 2
I am working on an exercise in which I am to "write a method to compute the sum of the digits in an integer". (Example, sumDigits(234) returns 2+3+4=9) I came up with the following code which does work, but surely there is a better way. I am very new to JAVA and not very familiar with the API. I figure there must be something in there to help with this method implementation. I may have even seen what I need and didn't recognize it. Any hints as how to proceed?
Thanks.
[CODE}
public static long sumDigits(long n)
{
int times = 0;
long sum = 0;
long number = n;
long divisor = 1;
long quotient = 0;
long digit = 0;

while ((number / divisor) > 10)
{
divisor *= 10;
times += 1;
}

while (times >= 0)
{
digit = number / divisor;
number -= (digit * divisor);
sum = sum + digit;
divisor /= 10;
times -= 1;
}

return sum;
}
[/CODE]
Allen Alchian
Ranch Hand

Joined: Oct 11, 2000
Posts: 83
Warren
I don't know if this is any better than what you did, but here's the way I'd approach it.
I'd look at the String class.
Method length() returns an int which gives the number of characters of the current object.
Method charAt(int i) returns the character at index i.
So, using these two methods and some loops, plus conversion of char type back to int type, I think you could accomplish the task.
Does this help at all? It's an intersting little project...I might give it a try myself.


Allen
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 582
    
    7

Warren,
Following is an example, see if it meets you needs.

steve
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 582
    
    7

Warren,
Of if you wanted to make a separate method, and pass a long:

steve

[This message has been edited by Steve Fahlbusch (edited October 13, 2000).]
Warren Oldham
Greenhorn

Joined: Oct 13, 2000
Posts: 2
I just wanted to say thanks to Allen and Steve for taking the time to post a reply to my little problem.
Steve's use of the modulus operator really cleaned up the method I came up with. Since I'm trying to become better acquainted with the Java API, I'll also be trying Allen's idea of using the String class.
Thanks again guys. I really appreciate it.
Warren
Dipu Krishnankutty
Greenhorn

Joined: Oct 16, 2000
Posts: 10
The solution seems to be good.
But what i would think better would be to take the number as a String.use the String.charAt(i)
eg:
String s=t1.getText();
int sum=0;
for(int i=0;i<s.length(;i++)>
{
sum+=Integer.parseInt(s.charAt(i));
}
t2.setString(Integer.toString(sum));
Allen Alchian
Ranch Hand

Joined: Oct 11, 2000
Posts: 83
I might as well get my solution posted, too. So many ways to accomplish the same thing!
class sod
{
static long sod(String input)
{
long total = 0;
for ( int i = 0; i < input.length(); i++ )
{
total += Character.digit(input.charAt(i),10);
}
return total;
}
public static void main( String args[])
{
if (args.length == 1)
{
System.out.println( "You entered " + args[0] );
System.out.println( "Sum of the digits = " +
sod( args[0] ) );
}
else
{
System.out.println( "Usage: sod nnn\n
where nnn is any sequence of digits" );
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sum Of Digits