1. Negating a BigDecimal

I am making a calculator. I heard that the BigDecimal class was the best to use for precision. I tried negating a BigDecimal( using the negate() and multiply(-1) method ) and outputting it in a textField. For some reason the output doesn't come out negative. Could someone help me out?

xInput and yInput are the textFields while x and y are the variables.

Java Code:
```else if(command == "+/-"){
if(xInputSelected){
BigDecimal bigDecimal1 = new BigDecimal(xInput.getText());
x = bigDecimal1;
x.negate();
xInput.setText(x.toString());
}
else{
BigDecimal bigDecimal2 = new BigDecimal(yInput.getText());
y = bigDecimal2;
y.multiply(new BigDecimal(-1));
yInput.setText(y.toString());
}```

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
When processing commands - or trying to! and finding the unexpected - use System.out.println() to see what is happening or not.

Java Code:
```[color=blue]System.out.println("command==" + command);[/color]
//...

else if(command == "+/-"){
[color=blue]System.out.println("Processing +/- command");[/color]
if(xInputSelected){```

-----------------

In this case the expression command=="+/-" is comparing two strings with ==. Don't do that because it is comparing something like "indentity" rather than "being made up of the same characters in the same order" which is our normal way of thinking about string equality.

Strings - like other classes - usually define their own equals() method to make precise what they mean by equality. Usually this method should be used for Strings and other classes. Try this:

Java Code:
`else if(command.equals("+/-")){`

3. Originally Posted by pbrockway2
When processing commands - or trying to! and finding the unexpected - use System.out.println() to see what is happening or not.

Java Code:
```[color=blue]System.out.println("command==" + command);[/color]
//...

else if(command == "+/-"){
[color=blue]System.out.println("Processing +/- command");[/color]
if(xInputSelected){```

-----------------

In this case the expression command=="+/-" is comparing two strings with ==. Don't do that because it is comparing something like "indentity" rather than "being made up of the same characters in the same order" which is our normal way of thinking about string equality.

Strings - like other classes - usually define their own equals() method to make precise what they mean by equality. Usually this method should be used for Strings and other classes. Try this:

Java Code:
`else if(command.equals("+/-")){`
Thanks for the tip. I changed all my "==" to ".equals". But I still have the problem. I used the System.out.println() method like you suggested. The number is not negated. :(

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
multiply() is not "inplace" - that is calling y.multiply() is not supposed to multiply y by anything. Rather it creates and returns a new BigDecimal value with the expected value. (This is common behaviour: the String method toLowerCase() does something similar).

So what you have to do is assign the returned value to a variable:

Java Code:
```else{
BigDecimal bigDecimal2 = new BigDecimal(yInput.getText());
y = bigDecimal2;
[color=blue]y = [/color]y.multiply(new BigDecimal(-1));
// or replace the two lines above with
// y = bigDecimal2.multiply(new BigDecimal(-1)
yInput.setText(y.toString());
}```

It's a good idea to check out the API docs in cases like this: although you might have missed the way multiply() works unless you were aware of the possibility. But looking at the docs reveals there is a useful negate() method...

5. Moderator
Join Date
Apr 2009
Posts
13,319
Rep Power
23
As obrockway is saying, BigDecimal and BigInteger (like String) are immutable. You cannot change the value in them, so all the methods in there (like add, subtract, multiply) return the result of that action, but have no effect on the particular object they've been called on.

6. Thanks for the help. These are two errors that I have ran into before. I should've recognized these.