java - JTable erroneous column summation values -


scenario :
jtable contains following data, have tried describe trying in image below :- enter image description here

so, guess able explain trying achieve here.

problem faced not display accurate results (summation), ofcourse. code have used :

public void doctotal_income(){    try{         int totc=8,xc=3,lc=4,ec=5, sc=6; // totc last column, xc-3rd, lc-4th , on...          for(int i=0;i<(easypath.doctorbusiness_table.getrowcount());i++){ // "easypath.doctorbusiness_table" table name            sumtot += double.parsedouble(easypath.doctorbusiness_table.getmodel().getvalueat(i, totc).tostring());            sumtotx += double.parsedouble(easypath.doctorbusiness_table.getmodel().getvalueat(i, xc).tostring());            sumtotl += double.parsedouble(easypath.doctorbusiness_table.getmodel().getvalueat(i, lc).tostring());            sumtote += double.parsedouble(easypath.doctorbusiness_table.getmodel().getvalueat(i, ec).tostring());            sumtots += double.parsedouble(easypath.doctorbusiness_table.getmodel().getvalueat(i, sc).tostring());        }        easypath.totalearnt_docbus_tf.settext(string.valueof(sumtot));        easypath.xtotincome_tf.settext(string.valueof(sumtotx));        easypath.ltotincome_tf.settext(string.valueof(sumtotl));        easypath.etotincome_tf.settext(string.valueof(sumtote));        easypath.stotincome_tf.settext(string.valueof(sumtots));         sumtot = 0;   // public static            sumtotx = 0;  // values globally        sumtotl = 0;  // declared        sumtote = 0;  // ,        sumtots = 0;  // initialised 0          }        catch(exception ex){            ex.printstacktrace();            joptionpane.showmessagedialog(null, "error in totalling income");        }    } 

i calling method doctotal_income() after refining jtable document listener (works fine) , firing eventlistener on jbutton.

private void jbutton6actionperformed(java.awt.event.actionevent evt) {                                              new doctor().doctotal_income(); // doctor class } 

after these irregular summations. guessing have gone wrong somewhere logic, there other thing missing?

i gladly appreciate suggestion on this. time

new doctor().doctotal_income(); // doctor class 

first of class names should start upper case character. "doctor()" should "doctor()".

why creating new doctor()?

if trying filter data in tablemodel need data table, not tablemodel.

so code should like:

jtable table = easypath.doctorbusiness_table;  for(int i=0; < table.getrowcount(); i++) {        sumtot += double.parsedouble(table.getvalueat(i, totc).tostring());        sumtotx += double.parsedouble(table.getvalueat(i, xc).tostring());        sumtotl += double.parsedouble(table.getvalueat(i, lc).tostring());        sumtote += double.parsedouble(table.getvalueat(i, ec).tostring());        sumtots += double.parsedouble(table.getvalueat(i, sc).tostring()); } 

Comments