• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A calculator question!

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


This is part of my calculator I'm trying to create. What i want to do is have the text field display the numbers which are being enterested into a the applet, and for them to be either added, multiplied, divided etc. So for example if a user wanted to add 120 to 120 they'd see "120 + 120" as the request, then the answer will be shown as "120 + 120= 240".

How will this be possible from here? All replies will be duely greatful from me, thanks

What I'm trying to do it
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

So what does this code do, then? If it doesn't do what you describe, tell us what it does, and what you're having problems with. Then we can try to help you get going again.
 
Bo Gambles
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically so far it has initiated buttons 0-9 with values, it also has given a clear function

The next part I want it to do is have some sort of key listener to allow multiple presses to bring up multiple numbers, because right now its brining up an int, and then it is just deleting the int when the secogn number is entered. For example it if I press button 1, it will show number one, but if I press button 2 it will clear the original number and then replace it with the number 2

Basically I want to make a simple calculator with key presses from the Jbuttons, but its working out to be a little hard
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of writing

aTextField1.setText("3");

you should do something like

aTextField1.setText(aTextField1.getText()+"3");

That way the previously entered text will be preserved.
 
Bo Gambles
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^^Thanks for that reply, I'm out of the labs right now but I will try it later today.
 
Bo Gambles
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, its me again!

Now I have the buttons and the text fields sorted out, but now I need to allow the buttons to do their calculations. Thats what Im having troubles with

Any help? Thanks
 
Ziyoda Mirkhonova
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bo, Gambles,
I am also creating calculator, and I have the same, almost the same problems.
You know, my problem is that these buttons that I'm telling get String, I mean, I say if you press that button, set Text "1". So, "1" is string, and I don't know how to make this string double. I did Double.toString and Double.parseDouble to convert it, but it says that it cannot be applied to Buttons.



How are you going to deal with this problem?

Please, help us!!!
 
Ziyoda Mirkhonova
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bo, Gambles,
I am also creating calculator, and I have the same, almost the same problems.
You know, my problem is that these buttons that I'm telling get String, I mean, I say if you press that button, set Text "1". So, "1" is string, and I don't know how to make this string double. I did Double.toString and Double.parseDouble to convert it, but it says that it cannot be applied to Buttons.



How are you going to deal with this problem?

Please, help us!!!
 
Bo Gambles
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to use a double.parsedouble(getText) and use that where you what to get your double from. You cant change a string into a double not using a parse, as far as Im aware
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic