• 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 Memory buttons not working?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my calculator code -



Any ideas why the memory buttons aren't working? Am i parsing an empty string here -



If i am how do i fix this problem? Thanks.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "not working"? What happens when you press the memory button? Does your app throw an exception? Does the "memory" contain the wrong value? Or does something else happen that is unexpected? Please give us more details about what happens and we can help you out.

With that said, you should add some System.out.println() calls (or find some other way) to display the values of the variables involved. This will often help you see what is happening in your code.

Layne
 
Joe Grimp
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It says "Error: Invalid Input" at the bottom of my applet once you press one of the memory function buttons. Has anyone got any ideas how i can get these buttons to work? Thanks.
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to intitialize the String object Arg. when you are initializing the buttons and labels and stuff...

i believe this will help you out..
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and use event.getSource()

arg = event.getSource()



if (arg == "buttonname")
{
//do this
}

//or


if(event.getSource() == "buttonname")
{
//do something
}

this will help your buttons out..... but instead of "implements actionlistener.

and you can also do:

JLabel Label;

if(event.getSource() == addButton)
{
int answer = (int) label.getText();

int count += answer; // initialize count at the top.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and use event.getSource()

arg = event.getSource()



if (arg == "buttonname")
{
//do this
}

//or


if(event.getSource() == "buttonname")
{
//do something
}

this will help your buttons out..... but instead of "implements actionlistener.

and you can also do:

JLabel Label;

if(event.getSource() == addButton)
{
int answer = (int) Label.getText();

int count += answer; // initialize count at the top.
// but have a clear button to clear the answer
// variable.
}
 
You've gotta fight it! Don't give in! Read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic