programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Finding the sum of digits

Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
I'm trying to find the sum of digits such a big number like 148148148148.
So, the result should be 52.
I'm using BigInteger to find the number using while loop, but the the result was 0.
I don't have an idea what I'm wrong.

This is the method that I have created. When I try to find the small number, not using BigInteger, it is working.
However, this method is not working properly.

Please let me know how to fix it.
Thanks.

Ranch Hand
Posts: 116
10
• 2
• Number of slices to send:
Optional 'thank-you' note:
Good morning Jiyoung.

Take a look at line 6... Where are you storing the result of the addition? Nowhere...
Now compare line 6 with what you did in line 7. See the difference?

Best regards
Carlos

Marshal
Posts: 8869
637
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch, Jiyoung Kim.

Please explain what you are doing there, step by step.
Seems that you over complicated that exercise. I could think of simpler approach, but that is kind of subjective.

You might would find easier simply to use Integer class and deal with one digit at a time. Could you guess what I have in mind?

Sheriff
Posts: 22784
131
• Number of slices to send:
Optional 'thank-you' note:
I think Integer could be a problem because of overflow. The same for Long.

I'd first try using String, and just inspecting each character at a time. If it's a number - fine, add it. If not - exception. You can use Character.digit(c, 10) to easily convert a character to an int (or -1 for non-numbers).

Sheriff
Posts: 17648
300
• Number of slices to send:
Optional 'thank-you' note:
What Carlos said was correct.

BigInteger instances are immutable, therefore all its methods that return a BigInteger return a new BigInteger object; the object you called the method on will retain the same value as before. You need to store the result of sum.add(temp) somewhere.

I don't think overflow is a big concern here. You'd have to be summing up the digits of a very very big number before you overflow a BigInteger.  You might want to operate on the absolute value of the number whose digits you're trying to add up though because mod() will not give you the expected digit if you're operating on a negative number. For example, if you have a BigInteger(-56), mod(10) will return 4, not 6.

Liutauras Vilda
Marshal
Posts: 8869
637
• Number of slices to send:
Optional 'thank-you' note:
I was thinking about simply iterating over the characters and parse them to individual single digit numbers and add them up. Mod seems to be an overkill for such big numbers.

Junilu Lacar
Sheriff
Posts: 17648
300
• 1
• Number of slices to send:
Optional 'thank-you' note:
I know it's against my "Don't optimize with your gut; use a profiler!" rule but I'm pretty sure that conversion of an integer value to String, then checking character by character is way more overkill than just straight up integer operations.  Edit: But then again, I was referring to primitive integer operations. It might be a whole 'nother story when you're using BigInteger. Still, I'm actually impressed the OP didn't resort to String manipulation which to me is actually a cop-out. I  even have interview questions (which I "borrowed" from somebody else) where you get bonus points for using all integer operations instead of String->char[]->sum(char[]).

Jiyoung Kim
Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
I solved it.. Thanks all!

Marshal
Posts: 79263
377
• Number of slices to send:
Optional 'thank-you' note:

Jiyoung Kim
Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note: