ard
ard

Reputation: 1

sum values of jtextfields in one jtextfields

I want to find the sum of the jtextfields (sht1 to sht13) and put the result in llog jtextfield, but it wont work if i dont put values in every jtextfield (sht1 to sht13) how can i fix my code so that it takes only the values of the jtextfields that i choose to put values on

private void llogMouseClicked(java.awt.event.MouseEvent evt) {                                  
      try{
      int sht1 = Integer.parseInt(vl1.getText());
      int sht2 = Integer.parseInt(vl2.getText());
      int sht3 = Integer.parseInt(vl3.getText());
      int sht4 = Integer.parseInt(vl4.getText());
      int sht5 = Integer.parseInt(vl5.getText());
      int sht6 = Integer.parseInt(vl6.getText());
      int sht7 = Integer.parseInt(vl7.getText());
      int sht8 = Integer.parseInt(vl8.getText());
      int sht9 = Integer.parseInt(vl9.getText());
      int sht10 = Integer.parseInt(vl10.getText());
      int sht11 = Integer.parseInt(vl11.getText());
      int sht12 = Integer.parseInt(vl12.getText());
      int sht13 = Integer.parseInt(vl13.getText());
 if (vl1.getText()==null||vl2.getText()==null||vl3.getText()==null||vl4.getText()==null||vl5.getText()==null||vl6.getText()==null||vl7.getText()==null||vl8.getText()==null||vl9.getText()==null||vl10.getText()==null||vl11.getText()==null||vl12.getText()==null||vl13.getText()==null){
    sht1=0;
    sht2=0;
    sht3=0;
    sht4=0;
    sht5=0;
    sht6=0;
    sht7=0;
    sht8=0;
    sht9=0;
    sht10=0;
    sht11=0;
    sht12=0;
    sht13=0;
    int sum= sht1 + sht2 + sht3 + sht4 + sht5 + sht6 + sht7 + sht8 + sht9 + sht10 + sht11 + sht12 + sht13;
  llog.setText(""+ sum);}

Upvotes: 0

Views: 513

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Suggestions:

  • Put your JTextFields into an ArrayList<JTextField>
  • In the code above, use a for loop to iterate through this list.
  • Inside the loop, parse the field. If the parse fails, ignore the NumberFormatException -- but most importantly, don't try to add any results (which don't make sense), into your sum. In other words, add the parsed value to your sum within the try block, and leave the catch block empty (one of the few times it's perhaps OK to do this).

e.g.,

// note that likely you should be using an ActionListener not a MouseListener
private void myButtonActionPerformed(java.awt.event.ActionEvent e) {
    int sum = 0;
    // assuming a JTextField ArrayList<JTextField> called vFieldList
    for (JTextField  vField : vFieldList) {
        String text = vField.getText().trim();  // get text and trim out all outside whitespace
        try {
            int value = Integer.parseInt(text);

            // if exception thrown, this line won't be reached
            sum += value;  // add value to sum
        } catch (NumberFormatException nfe) {
            // ignore exception
        }
    }

    llog.setText("" + sum);
}

Do you see that by using a list and a for loop, how this simplifies the code, making it much easier to debug or enhance?

Better still:

  • Don't use JTextFields at all, but rather bunch JSpinners that you initialize to 0.

Upvotes: 1

Related Questions