从JTable解析的双打的总和 [英] Sum of Doubles that where parsed from a JTable
问题描述
如何获得的双重数值
从 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.
我想得到值的总和在 JTable
中的列中,此列包含 String
对象,其中#,##
格式,所以当我尝试用一个循环解析它们加倍,我得到这个错误:
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"
这里是$ c> JTable image:
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);
}
推荐答案
我认为问题是数字中的逗号。您应该尝试使用 NumberFormat class。
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();
}
}
这篇关于从JTable解析的双打的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!