简体   繁体   中英

Sum of Doubles that where parsed from a JTable

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

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

EG:

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM