Results 1 to 2 of 2

Thread: subtracting the last number from a string

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    4

    subtracting the last number from a string

    Hi, i'm creating a basic calculator application with NetBeans 6.0 and I'm having trouble getting the backspace button to work. I've been trying the code

    display = (display.length -1);

    where display is the number that the user input, but i'm getting the error of

    "operator - cannot be applied to java.lang.string.length,int"

    Display is set as a string and later converted to a double..


    code dealing with the display variable is:
    edit: top part of the code with all the display = is variations i was trying to play around with
    Code:
    private void backspace(String display)
    {
       // display = display.Remove(display.Length - 1);
        display = String.valueOf(display);
        display = ((display.length)- (one));
        display = (display.length -1);
        
    }
    
    private void operation(char currentOp)
    {
       if (!operatorPressed)
      {
            switch( previousOperation )
            {
                case 'B':
                    previousDisplay = display;
                    break;
                case '=':
                    previousDisplay = String.valueOf(
                            Double.parseDouble(previousDisplay)
                            +
                            Double.parseDouble(display));
                    display = previousDisplay;
                    jTextField.setText(display);
                    break;
                case '+':
                    previousDisplay = String.valueOf( 
                            Double.parseDouble(previousDisplay) 
                            +
                            Double.parseDouble(display));
                    display = previousDisplay;
                    jTextField.setText(display);
                    break;
                case '-':
                    previousDisplay = String.valueOf( 
                            Double.parseDouble(previousDisplay) 
                            -
                            Double.parseDouble(display));
                    display = previousDisplay;
                    jTextField.setText(display);
                    break;
                case '*':
                     previousDisplay = String.valueOf(
                            Double.parseDouble(previousDisplay)
                            *
                            Double.parseDouble(display));
                    display = previousDisplay;
                    jTextField.setText(display);
                    break;
                case '/':
                     previousDisplay = String.valueOf(
                            Double.parseDouble(previousDisplay)
                            /
                            Double.parseDouble(display));
                    display = previousDisplay;
                    jTextField.setText(display);
                    break;
            }
            if (currentOp == '=')
            {
                display = "0";
                previousDisplay = "0";
                previousOperation = 'B';
            }
            previousOperation = currentOp;
            operatorPressed = true;
        }
    }
    also, my decimal isn't working correctly.. if i do 9.6 + .4 it shows 9.6. + 4 = 13.6 - any ideas?
    Code:
    private void decimaldot(String num)
    {
        jTextField.setText("0");
        jTextField.setText(display + '.');
        display = display + '.';
    
    }
    thanks in advance
    Last edited by veedubdan74; Mar 28th, 2009 at 04:36 PM.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: subtracting the last number from a string

    Use display.length() because length is a method not an attribute
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width