I have three JTextfields called vol1HH1,vol1HH2,vol1HH3 and fvol. The result of which is output after an actionPerformed event.
A user may just input a numbers into vol1HH1 and vol1HH2 but not vol1HH3, how would i ensure that the two numbers were added and the result displayed into fvol without causing a number handling error by the empty JTextField vol1HH3?
public void actionPerformed(ActionEvent e) {
double number1 = Double.parseDouble(vol1HH1.getText());
double number2 = Double.parseDouble(vol1HH2.getText());
double number3 = Double.parseDouble(vol1HH3.getText());
fvol.setText(Double.toString(number1 + number2 + number3));
}
I tried this but this doesnt seem to work, what am i doing wrong?
double sum = 0;
if (!vol1HH1.getText().trim().isEmpty()||!vol1HH2.getText().trim().isEmpty()){
// only now parse the text and add it
sum += Double.parseDouble(vol1HH1.getText());
sum += Double.parseDouble(vol1HH2.getText());
}
fvol.setText(Double.toString(sum));