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