Search code examples
javajtablesqldatatypes

java.math.BigDecimal cannot be cast to java.lang.String?


I am trying to add row values of datatype(money) in which zeros after decimal are automatically formed in Jtable like these 55.0000 28.0000 60.0000 20.0000 50.0000

on runtime log showing this error

java.math.BigDecimal cannot be cast to java.lang.String

Here is the code for rows addition from Jtable

        double total = 0;
        for(int i = 0; i<table.getRowCount(); i++){
            double amount = Double.parseDouble((String) table.getValueAt(1, 6) );
            total+=amount;
        }
        totalSum.setText(String.valueOf(total));

*

Is there any way to add float type values??

    *

Solution

  • Try something like this:

    BigDecimal total = BigDecimal.ZERO;
    for (int i = 0; i < table.getRowCount(); ++i) {
        final BigDecimal amount = (BigDecimal)table.getValueAt(i, 6);
        total = total.add(amount);
    }
    totalSum.setText(total.toString());