permaculture playing cards*
The moose likes Java in General and the fly likes caluculating sum of all the numbers in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "caluculating sum of all the numbers in a String" Watch "caluculating sum of all the numbers in a String" New topic
Author

caluculating sum of all the numbers in a String

dhruthi sharma
Greenhorn

Joined: Dec 05, 2011
Posts: 2


Hi ,
In the recent interview the interviewer asked me the logic where i have to calculate the total sum of the numbers with in the string.
String str = "5 10 44 55";// generic

can anybody help me out for the above logic.

Thanks.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi Dhruthi,

Welcome to the Ranch

So what was the logic you thought of when you were asked the question?
dhruthi sharma
Greenhorn

Joined: Dec 05, 2011
Posts: 2


Hi John,

I tried the below logic,
first i checked the length of the string.
ie
int len = str.lenght();
String s1[] = str.split(" ");
in the next step i traverse the loop,
for(int i-0;i<len;i++)
{
s1[i]= s1[i]+s1[i+1];
//here i struck ed with the logic

}

If possible can you advise how exactly i can do this.

Many Thanks,
dhruthi
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Look at the parseInt method of the Integer class which will take a string a convert it to an int (watch for exceptions if you give it a string which is not an int!). Also, watch your loop - the logic in your example is updating the array elements (each of which is a string). Consider an int variable initialized to 0 and increment it with the parsed values from the array.

Also consider what your total value will be, if you exceed the maximum int value (or I guess fall below the minimum value if you are working with negative numbers) you may need to use a long to total the values.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

dhruthi sharma wrote:first i checked the length of the string...

Your first mistake. What does str.length() return?

String s1[] = str.split(" ");

Seems reasonable.

in the next step i traverse the loop,
for(int i-0;i<len;i++)

Not only is the logic mistaken, but the above statement won't compile.

s1[i]= s1[i]+s1[i+1];

OK. Several problems here:
1. s1 is a String array, so s1[i] is a String. What does String + String return you?
2. You're assigning the result to s[i]. There's nothing syntactically with that, but what do you think is going to result?

If possible can you advise how exactly i can do this.

At the risk of sounding glib: do it correctly. Right now, you're just banging down code without thinking through the problem.

My advice: Write out in english (or your native language) the steps you need to follow to sum all the numbers in a String. You've already identified some of them, but you're diving into Java before you've got the entire problem sorted out in your head - and that's usually a recipe for trouble.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

Hi Dhruthi,

Given your example, what would be the length of the string "5 10 44 55" ?
Do you need to iterate so many times ?

Secondly, be aware that s1 is an array of Strings, so using the + operator would concatenate the Strings, and not add the underlying numbers.

Claudiu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: caluculating sum of all the numbers in a String
 
Similar Threads
Prime numbers selected at random
want to implement sorting logic in my project with jsf
To find a largest sector in a given input string of 1's and 0's
Regarding Collection
Project Euler : Problem 23