[英]Sum of Doubles that where parsed from a JTable
如何獲得從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);
}
我認為問題在於數字中的逗號。 您應該嘗試使用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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.