This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A simple calculator help!

 
Bo Gambles
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again

I've got a quick calculator that I wanted to create, but I seem to be having more troubles with it than I expected



But I'm having troubles doing the calculation. I can't get the String A number to recall it in the equals part. I'll be extremly grateful if someone could clean the code up if they have the time, or give me a general code that I can use to do the calculation.

If this isn't possible, could anyone give me examples of some REALLY basic and simple calculator code, thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your handlers for add, minus and multiply you have lines like

int firstInput = Integer.parseInt(aTextField1.getText());

That "int" in front of it should not be there. You're declaring a new variable firstInput, which hides the field of the same name. Thus the newly entered value is never stored in a place where the code can get at it.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok....you should only have one listener for all those buttons..

just do an if - else statement..

like so

public class listener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == one)
label.setText("1");

if(event.getSource() == two)
label.setText("2");

//and so on

}

}

-Justin-
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!