0

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));
Ingram
  • 654
  • 2
  • 7
  • 29
  • Please clarify, `"but this doesn't seem to work"`. – Hovercraft Full Of Eels Jun 21 '15 at 20:08
  • Also walk through your program logic on paper. Logic issues such as `||` and the NOT operator should be fixable by you without our help. I have confidence that you can solve this. – Hovercraft Full Of Eels Jun 21 '15 at 20:11
  • Consider using a `JSpinner` or `JFormattedField` which can perform there own validation. See [How to Use Spinners](http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html) and [How to Use Formatted Text Fields](http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html) for more details – MadProgrammer Jun 22 '15 at 01:20

2 Answers2

2
  1. Ensure that only numbers can be added to your JTextFields -- a DocumentFilter added to the JTextField's Document would work well for this. For more on this, please see my answer to a similar question.
  2. Test if the JTextField is empty before trying to parse it by use of a simple if block:

if (!myTextField.getText().trim().isEmpty()) {
   // only now parse the text and add it
   sum += Double.parseDouble(myTextField.getText());
}
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @MrAssistance: ouch my eyes. Please avoid trying to post code in comments unless you're posting a fragment of a single line of code. Instead if you need to give us more code to review, edit your original question and add any new code to the bottom of the question. With regards to `"would something like this work?"` the best way to find out is to try it out and see. – Hovercraft Full Of Eels Jun 21 '15 at 19:55
  • @MrAssistance: your boolean logic is off. Again, write out the logic on paper, step through the logic in your mind, and you'll figure it out. – Hovercraft Full Of Eels Jun 21 '15 at 20:39
1

To avoid passing empty string to the Double.parseDouble() method, Why don't you use a conditional operator?

May be something like double number3 = Double.parseDouble(vol1HH3.getText().equals("") ? "0" : vol1HH3.getText()); would help you with the problem I guess.

whenever the field is empty the vol1HH3.getText() returns an empty string and passing a 0 at that time would get the work done and will not affect your total sum.