Please help me in writing a Java program to find sum of numbers in a random string.(Input=abc235^%$q!12&3 output=250)
If a number has - sign then u should consider it as a negative number.(Input=abc-12t%^&$dcf22 Output=10)
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Please help me in writing a Java program to find sum of numbers in a random string.(Input=abc235^%$q!12&3 output=250)
If a number has - sign then u should consider it as a negative number.(Input=abc-12t%^&$dcf22 Output=10)
What have you tried?
If you don't understand my answer, don't ignore it, ask a question.
I tried parsing the string and store the number in another string.Once the number start i store it in another string,i keep on appending until I get anything else other than integer.Then I convert the number in string to integer and add it to variable sum. I tried this logic but not able to write correct code. Please send me optimize solution for this problem in Java.
Can you post the code you are having problems with?
Is this what you are trying to do?
Input = "2x3x4"
output = 9
If you don't understand my answer, don't ignore it, ask a question.
Your logic sounds correct.
If you need help with the code you should read the API for String objects in Java.
String Documentation: String (Java Platform SE 6)
Especially the methods "length()", "charAt(int i)" and "substring(int from, int to)" could be useful to you.
If you are still struggling with it after reading the API you might also try to write it in Pseudo-Code and post that; perhaps we can help you translate it into java.
Another way is to use regular expression and find sequence matching in given string and then performing addition operation.
Welcome Mr manebrahma to the forum. Please read this topic to learn how to post code in code or highlight tags and other useful info for new members. If you have any problem regarding your programs, please post your problems at What's Wrong With My Code? But I guess you already did that part.
Thank You for your post.