# Simple question...I hope

Printable View

• 01-07-2011, 06:23 PM
marksey07
Simple question...I hope
Code:

```public class Calculator2 {         public static void main(String[] args){                 double result = result_of("17+28-11");                 System.out.println(result);         }         public static double result_of(String equation)         {                 if(equation.contains("+"))                 {                         //equation.split not String.split                         String[] split_up_equation = equation.split("[+]");                         double result = 0.0;                         for(int i=0;i<split_up_equation.length;i++)                         {                                 //add the double value                                 result += result_of(split_up_equation[i]);                         }                         return result;                 }                 if(equation.contains("-"))                 {                         String[] split_up_equation = equation.split("[-]");                         double result = 0.0;                         for(int i=0;i<split_up_equation.length;i++)                         {                                 result += result_of(split_up_equation[i]);                         }                         return result;                 }                 return Double.valueOf(equation);         } }```
This is my code for a calculator, as you can see the method for"-" adds rather than takes away. I don't believe there is a -= rather than +=

Can anyone help me
• 01-07-2011, 06:27 PM
JosAH
Quote:

Originally Posted by marksey07
This is my code for a calculator, as you can see the method for"-" adds rather than takes away. I don't believe there is a -= rather than +=

Yup, there's a -= operator for numbers: a-= b subtracts the value b from the value a and assigns it to a again, no problem.

kind regards,

Jos
• 01-07-2011, 06:28 PM
KevinWorkman
Quote:

Originally Posted by marksey07
I don't believe there is a -= rather than +=

What happened when you tried? Keep in mind that you don't have to use the += operator; it's just a shortcut for doing something like this:

Code:

```int x = 7; x = x + 2; // same as x += 2;```
PS- In the future, you might want to use a more meaningful post title. People tend to ignore posts with generic titles like this one.

Edit- Too slow! But recommended reading: http://download.oracle.com/javase/tu...bolts/op1.html
• 01-07-2011, 06:29 PM
marksey07
so
Code:

```if(equation.contains("-"))                 {                         String[] split_up_equation = equation.split("[-]");                         double result = 0.0;                         for(int i=0;i<split_up_equation.length;i++)                         {                                 result -= result_of(split_up_equation[i]);                         }                         return result;                 }                 return Double.valueOf(equation);```
and
Code:

`double result = result_of("50-20");`
should return 30?
• 01-07-2011, 06:32 PM
JosAH
Quote:

Originally Posted by marksey07
Code:

`double result = result_of("50-20");`
should return 30?

I don't know what your result_of( ... ) method does but if it is even half sane I'd expect that result yes. What's keeping you from trying the code, don't take my word for it.

kind regards,

Jos
• 01-07-2011, 06:37 PM
KevinWorkman
Quote:

Originally Posted by marksey07
so
Code:

```if(equation.contains("-"))                 {                         String[] split_up_equation = equation.split("[-]");                         double result = 0.0;                         for(int i=0;i<split_up_equation.length;i++)                         {                                 result -= result_of(split_up_equation[i]);                         }                         return result;                 }                 return Double.valueOf(equation);```
and
Code:

`double result = result_of("50-20");`
should return 30?

Again- what happened when you tried it?
• 01-07-2011, 06:40 PM
marksey07
OK, didn't work. But im just going to have a play, I have a good feeling I'm going to get there :) :eek:

Thanks guys