• Post Reply Bookmark Topic Watch Topic
  • New Topic
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

caluculating sum of all the numbers in a String

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.

 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 75
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic