posted 7 years ago

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.

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.

posted 7 years ago

Welcome to the Ranch.

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.

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.