Sum of Doubles that where parsed from a JTable

2019-08-04 06:52发布

How to get the sum of Doubles that where parsed from a JTable?

I'm Building a Client/Server Application, and I'm Stuck on a Little issue.

I want get the sum of values of a column in a JTable, this column contains String objects with #,## format, so when I try to parse them into doubles with a loop I get this error:

java.lang.NumberFormatException: For input string: "0,55"

Here is the JTable image:

enter image description here

And here is the code that I tried so far:

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt)    {                                          

    Double summl = 0.0;

    for(int i = 0; i < jTable1.getRowCount(); i++){
        summl=summl+Double.parseDouble(jTable1.getValueAt(i,5).toString());
    }
     System.out.println("summl = "+summl);
   }  

1条回答
迷人小祖宗
2楼-- · 2019-08-04 07:20

I think the problem is with the comma in the number. You should try to parse the numbers using NumberFormat class.

E.G.:

public static void main(String[] args) {
        NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH); //Set the proper locale here

        try {
            Double myDouble = (Double)nf.parse("0,55");
            System.out.println(myDouble);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
查看更多
登录 后发表回答