Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Calculator clear button not working... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculator clear button not working..." Watch "Calculator clear button not working..." New topic
Author

Calculator clear button not working...

Dianne Calhoun
Greenhorn

Joined: Sep 25, 2001
Posts: 29
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).]


Thanks, Dianne
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
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

Joined: Sep 25, 2001
Posts: 29
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
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Sep 25, 2001
Posts: 29
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

Joined: May 17, 2001
Posts: 2823
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculator clear button not working...
 
Similar Threads
This Keyword
divide by 0 exception
pls help adding menu bar to prgram
NullPointerException - Using JTextField to Add Numbers