|
-
Mar 28th, 2009, 04:01 PM
#1
Thread Starter
New Member
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.
-
Mar 29th, 2009, 01:58 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|