Help needed please
My task is to create an applet that has the following :
2 editable textFields for entering numers
1-non-editable TextField to display the result of calculation and error messages.
An Add button which adds the two numbers and displays the result or an error message in the result textField when clicked.
catches any errors using a try catch block and displays an appropriate error message in the result box
I am having trouble with my code as when i compile it, it runs fine but it is not adding the two numbers together.
I realise that the error is in the action performed method. Could someone please take a look at my code and point me in the right direction please
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
publicclass ExceptionDemo1extends Appletimplements ActionListener
{
private TextField tF1;
private TextField tF2;
private TextField resultField;
private Label resultLabel, stringLabel;
private Button badd;
publicvoid init()
{
stringLabel =new Label("Type an integer: ");
resultLabel =new Label("Answer: ");
tF1 =new TextField(20);
tF2 =new TextField(20);
resultField =new TextField(20);
resultField.setEditable(false);
Button badd =new Button("Add");
badd.addActionListener(this);
add(stringLabel);
add(tF1);
add(tF2);
tF1.addActionListener(this);
tF2.addActionListener(this);
add(resultLabel);
add(resultField);
add(badd);
}// end init
publicvoid actionPerformed(ActionEvent event)
{
if (event.getSource() == badd)
{
try
{
int number = Integer.parseInt(tF1.getText());
int number1 = Integer.parseInt(tF2.getText());
resultField.setText("The Sum Total is " + number*number1);
}
catch (NumberFormatException e)
{
resultField.setText("Error in number: retype ");
}
}// end if
}// end actionPerformed
}//e
nd exceptionDemo1
Well, if you're looking to add values, why are you multiplying them?If there is any other problem, please explain what it is and post any error message you get.
Yes sorry, i am trying to add the values, i tried to multiply them to see if i got a different result.
I am not getting an error message.
When i run the program and enter numbers into tf1 and tf2 they appear no problem. It is when i click the add button nothing happens. ie, the numbers are not added together.
Hope this explains it properly
this is my code as i have it adding the two numbers from the textfields
When i type in the numbers and try to add them when clicking the add button, nothing happens.Could someone please point out why this is not happening
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ExceptionDemo1 extends Applet implements ActionListener
{
private TextField tF1;
private TextField tF2;
private TextField resultField;
private Label resultLabel, stringLabel;
private Button badd;
public void init()
{
stringLabel = new Label("Type an integer: ");
resultLabel = new Label("Answer: ");
tF1 = new TextField(20);
tF2 = new TextField(20);
resultField = new TextField(20);
resultField.setEditable(false);
Button badd = new Button("Add");
badd.addActionListener(this);
add(stringLabel);
add(tF1);
add(tF2);
tF1.addActionListener(this);
tF2.addActionListener(this);
add(resultLabel);
add(resultField);
add(badd);
} // end init
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == badd)
{
try
{
int number = Integer.parseInt(tF1.getText());
int number1 = Integer.parseInt(tF2.getText());
resultField.setText("The Sum Total is " + number+number1);
}
catch (NumberFormatException e)
{
resultField.setText("Error in number: retype ");
}
}// end if
} // end actionPerformed
}//end exceptionDemo1
Message was edited by:
fowlergod09
In your actionPerformed method write the followingButton btn = (Button) event.getSource(); then check if (btn == badd) bye for nowsat
> In your actionPerformed method write the following
> > Button btn = (Button) event.getSource();
>
>
then check if (btn == badd)
>
> bye for now
> sat
Why would you do that.. there's no need to add that cast as it might cause trouble when new components are added to the interface that also implement the ActionListener interface.
I am sorry mate but i am not sure i understand what you mean.
Edit:
I have tried that but it is making no difference:
when i click on the add button nothing is happening.The result of the two numbers entered in tf1 and tf2 should be added together and should be displayed in the result Textfield.
This is not happening
Message was edited by:
fowlergod09
@OP: sprinkle in a few System.out.println() calls and see if your
ActionListener actually gets called; the beginning of your actionPerformed
method might look like this then:public void actionPerformed(ActionEvent event) {
System.out.println("actionPerformed called ok");
if (event.getSource() == badd) {
System.out.println("source is ok");
try {
...
kind regards,
Jos
JosAHa at 2007-7-11 22:26:33 >

@ simeon, you are correct. I didnt think about that
hi add anyone of the following in your init method. I am not sure which method applet supports. I guess you didnt setsize. try this.
setSize(100,200)
//or
pack();
bye for now
sat
My problem now is that when i click the add button instead of doing the calculation of adding the two numbers together from the textfields it is just putting the two numbers together.
my code is as follows:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ExceptionDemo1 extends Applet implements ActionListener
{
private TextField tF1;
private TextField tF2;
private TextField resultField;
private Label resultLabel, stringLabel;
private Button badd;
public void init()
{
stringLabel = new Label("Type an integer: ");
resultLabel = new Label("Answer: ");
tF1 = new TextField(20);
tF2 = new TextField(20);
resultField = new TextField(20);
resultField.setEditable(false);
Button badd = new Button("Add");
badd.addActionListener(this);
add(stringLabel);
add(tF1);
add(tF2);
tF1.addActionListener(this);
tF2.addActionListener(this);
add(resultLabel);
add(resultField);
add(badd);
} // end init
public void actionPerformed(ActionEvent event)
{
System.out.println("actionPerformed called ok");
if (event.getSource() == badd)
System.out.println("source is ok");
{
try
{
int number = Integer.parseInt(tF1.getText());
int number1 = Integer.parseInt(tF2.getText());
resultField.setText("The Sum Total is " + number+number1);
}
catch (NumberFormatException e)
{
resultField.setText("Error in number: retype ");
}
}// end if
} // end actionPerformed
}//end exceptionDemo1
> My problem now is that when i click the add button
> instead of doing the calculation of adding the two
> numbers together from the textfields it is just
> putting the two numbers together.
> resultField.setText("The Sum Total is" + number+number1);
Yup, Java always evaluates expression from left to right (taking care of
operator precedence). The expression "foo"+1+2 (for example) is
evaluated as:
("foo"+1)+2 == "foo1"+2 == "foo12"
A few simple parentheses take care of that: "foo"+(1+2)
"foo"+(1+2) == "foo"+3 == "foo3"
kind regards,
Jos
ps. how come your actionPerformed method suddenly *is* called?
JosAHa at 2007-7-11 22:26:33 >

Instead of :resultField.setText("The Sum Total is " + number+number1);use:resultField.setText("The Sum Total is " + ( number+number1));****, JosAH was quicker:)Message was edited by: bytetron
My action performed is called when the button add was clicked. I am still very new to java so forgive me if i have misunderstood you question. The adding of the parentheses has worked, thankyou all for your help.
Here is an example of a similar program I made. I think that you should be keeping the action you expect badd to perform in it's own method. Here is an example:
private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {
//first we define float variables
float num1, num2, result;
//we have to parse the text to a type float
num1 = Float.parseFloat(jTextField1.getText());
num2 = Float.parseFloat(jTextField2.getText());
result = num1 + num2;
// We will now pass the value of result to jTextField3.
// At the same time, we are going to
// change the value of result from a float to a string.
jTextField3.setText(String.valueOf(result));
}
Hope this helps!
Mike