aspose file tools*
The moose likes Beginning Java and the fly likes A calculator question! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A calculator question!" Watch "A calculator question!" New topic
Author

A calculator question!

Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14


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
Marshal

Joined: Mar 22, 2005
Posts: 42294
    
  64
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.


Ping & DNS - my free Android networking tools app
Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14
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
Marshal

Joined: Mar 22, 2005
Posts: 42294
    
  64
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

Joined: Mar 27, 2006
Posts: 14
^^Thanks for that reply, I'm out of the labs right now but I will try it later today.
Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14
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

Joined: Mar 16, 2006
Posts: 10
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

Joined: Mar 16, 2006
Posts: 10
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

Joined: Mar 27, 2006
Posts: 14
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A calculator question!