wood burning stoves*
The moose likes Beginning Java and the fly likes Sum of digits in alphanumeric string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sum of digits in alphanumeric string" Watch "Sum of digits in alphanumeric string" New topic
Author

Sum of digits in alphanumeric string

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
I have an alphanumeric string. I need the sum of all of the digits in this string. What is the best and most efficient way to find the sum of all digits. Please suggest the solution which can be considered generic for all programming languages.
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
The solution is going to change depending on which language you're using. Check out the String API for useful functions. You might also want to check out the Integer API.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Thanks for your reply.

I know solution is going to change based on the programming language. That is why i wrote that solution should be irrespective of any language.

So, we do not have to use any function which we think will be specific to java.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

The only way I can see is that you would have to transform each string in a int, and then sum it all. =/


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Raj Kumar Bindal wrote:Thanks for your reply.

I know solution is going to change based on the programming language. That is why i wrote that solution should be irrespective of any language.

So, we do not have to use any function which we think will be specific to java.


The problem is, it's pretty difficult to create a solution "irrespective of any language" other than in pseudocode. For example, how would you do this in whitespace, or lolcode?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Raj Kumar Bindal wrote:Please suggest the solution which can be considered generic for all programming languages.

There isn't a solution which is language-independent. If you're working in a procedural C-like language, such as Java for example, then you would go through the string one character and a time, converting the characters to numbers and adding them up. Whereas in a functional language like Lisp, you would more likely write a recursive function which added the number corresponding to the first character of the string to the value of that same function applied to the rest of the string.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

"Best" in terms of what - speed, memory, code complexity? Your question is meaningless without better defining what is most important.

Much of software design is a balance in the above mentioned items. If two people each think a different thing is the more important one, then their "best" solution will be vastly different.

And if you want a 'solution which can be considered generic for all programming languages', then that would be "sum all of the digits" - as you stated in your question.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
There is an ambiguity in the stated problem it would seem to me...How do you handle multiple consecutive digits? Like so:

abc12defg34ijk

If you sum all the digits you could have

12 + 34 = 46

or do you want to treat it as

1 + 2 + 3 + 4 = 10

Which is the answer you expect?
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
I will expect the answer like this:

1 + 2 + 3 + 4 = 10


We have to do some of all one digit numbers in the string.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You would have to go through the String, find each character, find out whether each character is a digit, find its numerical value, and add that.

There are methods in the String and Character classes for all those things, except adding.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sum of digits in alphanumeric string