This week's book giveaway is in the Cloud/Virtualizaton forum.We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# caluculating sum of all the numbers in a String

dhruthi sharma
Greenhorn
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
Rancher
Posts: 1776
• 1
Hi Dhruthi,

Welcome to the Ranch

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

dhruthi sharma
Greenhorn
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
Posts: 131
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
Posts: 10417
63
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

Claudiu Chelemen
Ranch Hand
Posts: 75
• 1
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