Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Multiplication problem

Hey guys,

I am brand new to Java and am trying to create a very simple first program. The program has 2 text boxes (txtInput & txtOutput) and a command button (cmdCalc). I would like the user to enter a number into txtInput and when the user clicks the command button that number is multiplied by a number detailed in the code (e.g. 5) and the calculation is displayed in the txtOutput text box.

So if the user entered 5 into the txtInput box and clicked the command button, 25 would appear in txtOutput box.

I am trying to familiarise myself with this simple example but I can't figure out how to code this calculation. Would anyone be able to help me out with the correct syntax and code?

Much appreciated, thanks.
[757 byte] By [nufc86] at [2007-11-11 8:18:02]
# 1 Re: Multiplication problem
Can you post the code that you have come up with? It would be very helpful to see the code so that I can understand what you're having trouble with.
destin at 2007-11-11 22:35:55 >
# 2 Re: Multiplication problem
Thats my problem unfortunately, I haven't got any code yet because I am unsure on what type of statement to use (if, do etc.)

Sorry about that mate.

I am guessing that what the user would enter into the txtInput box would have to be put into an array, but I'm not sure.
nufc86 at 2007-11-11 22:36:50 >
# 3 Re: Multiplication problem
String input = inTextField.getText();

int output = Integer.parseInt(input)*5; //is that what you can't figure out?

outTextField.setText();
Phaelax at 2007-11-11 22:37:48 >
# 4 Re: Multiplication problem
I'm getting there now, but there is still a problem. Here is a print screen of the simple form I've created.

http://img108.imageshack.us/img108/3209/java0zt.jpg

The code I've currently got is...

private void cmdClickActionPerformed(java.awt.event.ActionEvent evt) {
String input = txtInput.getText();
int output = Integer.parseInt(input)* 5;
txtOutput.setText("");
}

The code is compiling fine and the program runs, however the result of the calculation doesn't appear in txtOutput and because of that I am unsure on weather the calculation is even happening.

Any clues?

Thanks alot.
nufc86 at 2007-11-11 22:38:53 >
# 5 Re: Multiplication problem
your method inside the listener class should only be:

public void actionPerformed(ActionEvent e)

Implement ActionListener into a new inner class and implement the above method. Add a listener to the button.
Phaelax at 2007-11-11 22:39:52 >
# 6 Re: Multiplication problem
I thought that you couldn't change the code that is highlighted in blue.

I am unsure on how to make a new inner class, is there any chance that someone could make a small program like the one I've detailed with that multiplication so I can familiarise myself with the code. It may end up saving time.

I'd really appreciate it, thanks.
nufc86 at 2007-11-11 22:40:51 >