• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculator clear button not working...

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If someone could help me figure out how to get the clear button to work on my calculator, I would appreciate it.
Instructions: Write a program that displays a calculator. It has 1. an input field at the top to enter a number (20 characters wide). 2. display field that displays the current total (20 characters wide not editable text field). 3. 3 buttons "Add", "Subtract", "Clear". 4. Title of "Calculator". .
Calculator.java

This program works except for the clear button doesn't clear the field. Thanks for any help.
------------------
Thanks, Dianne

(Marilyn added code tags)
[This message has been edited by Marilyn deQueiroz (edited October 28, 2001).]
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never call it once the = is pressed. When you perform calculate the start boolean is set to true. You didn't put in the - sign so it just sets lastCommand to command.
ps try the - sign first before any numbers pressed then hit =.
[This message has been edited by Paul Stevens (edited October 29, 2001).]
 
Dianne Calhoun
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I did what you said correctly, here are my results.

It all compiles and looks great, but the clear button still does not clear the results field. Did I correct it the right way?
------------------
Thanks, Dianne
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dianne,
It has something to do with your 'start' variable. Inside your actionPerformed method you are using :
if( start )
{
}
else if( "Clear" )
{
}
Your start variable is always set so you never get to the else part. I don't have enough time to figure out what start is supposed to be ...
Regards,
Manfred.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if clear do the clear processing
else you do what ever
The start variable is messing your clear processing up. Did you try entering the - sign before anything else then the = sign?
 
Dianne Calhoun
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wanted to say thanks. My problem that I turned still did not work all the way, but it was really close. Paul I just wasn't understanding what you meant about the - and = comments. So, if you could elaborate on that I would appreciate it. Thanks everyone again.
------------------
Thanks, Dianne
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you typed the - sign followed by the = with no other numbers typed, an exception was thrown. It tried to parse the - as a number.
 
reply
    Bookmark Topic Watch Topic
  • New Topic