File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Simple Calculator help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Calculator help" Watch "Simple Calculator help" New topic

Simple Calculator help

Josh Thompson

Joined: Sep 10, 2010
Posts: 1
I have an assignment for my Java beginners class where I have to code the + JButton, -JButton, * JButton,and the / JButton. Only problem is I'm not sure how to start it. I know I have to obtain the user input from the first number JTextField and the second number JTextField. I've forgotten how to obtain that though. I'm thinking it's something like firstNumberJTextField.getText (); but when I do that NetBeans just throws up a red error box.

That's where I have to write the code to obtain the user input from both number JTextFields. I have to convert those numbers to int values and assign the number to int variables number1 and number2. Then I have to add the numbers and assign the result to the int variable result. Then display that number in the resultJtextField.

Much appreciated if anyone can help me with this.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10817

Welcome to the Ranch.

but when I do that NetBeans just throws up a red error box.

What does the error box say? Usually it will tell you what the problem is.

In pseudo code
1) Obtain the user input from text fields.
2) Your code will get these inputs as Strings as getText() returns a String object
3) Since you cannot "add" strings (in the mathematical sense), you will need to convert it to a corresponding Integer, Long or whatever your requirement is. The API docs for Integer etc. will show you how to do this conversion. (Hint: check the constructors of these classes)
4) Once you obtain "addable" entities, do the addition subtraction etc and obtain the result
5) Set the result back so the user can see it.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: Simple Calculator help
It's not a secret anymore!