Sum of Doubles that where parsed from a JTable

孤街醉人 提交于 2019-12-14 02:29:03

问题


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:

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:


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();
        }

    }


来源:https://stackoverflow.com/questions/8342069/sum-of-doubles-that-where-parsed-from-a-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!