Help with simple calculation

I'm slowly learning myself Java through JBuilder 6. I'm currently on with a currency convertor, one of my first programs that does more than say "Hello world". Anyway, this is a section of my code

void submit_actionPerformed(ActionEvent e) {

String s = jTextField1.getText();

try {

val = Double.parseDouble(s);

}

catch (NumberFormatException exp) {

val = 0;

}

if (jComboBox2.getSelectedItem() == "US Dollar") {

dResult = val * dDollar;

}

else if (jComboBox2.getSelectedItem() == "Pound Sterling") {

dResult = val * dPound;

}

else if (jComboBox2.getSelectedItem() == "Euro") {

dResult = val * dEuro;

}

else if (jComboBox2.getSelectedItem() == "Bahrain Dinar") {

dResult = val * dDinar;

}

else if (jComboBox2.getSelectedItem() == "Australian Dollar") {

dResult = val * dADollar;

}

else

dResult = val *dYen;

jTextField2.setText("" + dResult);

}

void jButton1_actionPerformed(ActionEvent e) {

jTextField2.setText("" + dResult);

}

-

The idea is for the user to enter their amount, select which currency they want from the combobox, then the result is displayed back to them. However, each time I run this program as it is, the result is always zero. I assume there's something wrong in the calculation, can anyone help me?

[1422 byte] By [MRJ3D] at [2007-9-27 16:48:01]
# 1

The code looks correct but one possibility is that the actionlistener submit_actionPerformed may not be registered and therefore not called.

I am assuming that your program calls jbutton1_actionPerformed instead.

Another tip. If there is numberformat exception , your program sets val to 0, and the goes through 5 tests which will all set dResult to 0.

robbfwhite at 2007-7-6 1:06:01 > top of Java-index,Archived Forums,Java Programming...
# 2

Thanks, how would I do that? I have this code in my program too (I haven't posted the whole thing)

private void jbInit() throws Exception {

this.setSize(new Dimension(400,300));

jButton1.setText("Convert");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton1_actionPerformed(e);

}

});

jTextField2.setText("Result");

jTextField2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jTextField2_actionPerformed(e);

}

});

I did try adding another actionListener, but the program would not work when it was inserted. Thanks for any help!

MRJ3D at 2007-7-6 1:06:01 > top of Java-index,Archived Forums,Java Programming...
# 3
jButton1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {submit_actionPerformed(e);}});
robbfwhite at 2007-7-6 1:06:01 > top of Java-index,Archived Forums,Java Programming...