【问题标题】:Sum of Doubles that where parsed from a JTable从 JTable 解析的双精度数之和
【发布时间】:2012-01-10 15:22:40
【问题描述】:

如何获得从JTable 解析的Doubles 的总和?

我正在构建客户端/服务器应用程序,但遇到了一个小问题。

我想得到JTable 中一列的值的总和,该列包含String 格式为#,## 的对象,所以当我尝试使用循环将它们解析为双精度时,我得到了这个错误:

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

这是JTable 图片:

这是我目前尝试过的代码:

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

【问题讨论】:

  • 模型列应该包含 Double 而不是 String

标签: java swing jtable double number-formatting


【解决方案1】:

我认为问题在于数字中的逗号。您应该尝试使用 NumberFormat 类解析数字。

例如:

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

    }

【讨论】:

  • 谢谢,这正是我一直想念的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 2019-06-14
  • 1970-01-01
  • 2012-03-11
  • 2016-03-03
  • 1970-01-01
相关资源
最近更新 更多