I want to get the sum of values of a column of the table on the click of a button, perform calculations, and then display results in the corresponding textfields.
I used the following code:-
int delivery = Integer.parseInt(DELIVERYCHG.getText());
int subtot = 0;
for (int i = 0; i <= ITEMDETAILSTABLE.getRowCount(); i++) {
String stot1 = (ITEMDETAILSTABLE.getValueAt(i, 4).toString());
int stot = Integer.parseInt(stot1);
subtot = subtot + stot;
}
SUBTOTAL.setText("" + subtot);
int subtotanddelivery = subtot + delivery;
int gst = (int) (0.18 * subtotanddelivery);
int total = subtotanddelivery + gst;
GST.setText("" + gst);
TOTAL.setText("" + total)
But output showed a error like this: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5 >= 5.
How can it be resolved?
Change:
int i = 0; i <= ITEMDETAILSTABLE.getRowCount(); i++
to
int i = 0; i < ITEMDETAILSTABLE.getRowCount(); i++
Indexes are counted from zero, so if 5 elements are in present, 4 is maximum index.