Results 1 to 6 of 6
  1. #1
    ryanmk54's Avatar
    ryanmk54 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Unhappy 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());
    	}
    Thanks in advance.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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. #3
    ryanmk54's Avatar
    ryanmk54 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    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. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    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. #6
    ryanmk54's Avatar
    ryanmk54 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Default

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

Similar Threads

  1. Divide bigdecimal again
    By ellhar in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 11:19 AM
  2. BigDecimal and BigInteger
    By jon80 in forum New To Java
    Replies: 8
    Last Post: 11-28-2010, 12:15 PM
  3. Using BigDecimal
    By doymand in forum New To Java
    Replies: 2
    Last Post: 10-30-2010, 10:25 AM
  4. BigDecimal question
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 02:44 PM
  5. BigDecimal - Help formulating if statments
    By shinjitsunohana in forum New To Java
    Replies: 5
    Last Post: 04-28-2008, 11:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •