将字符串数组列表存储到双数组列表中? [英] storing a string arraylist into a double arraylist?
问题描述
我有一个带有 ["51,073","51,074","51,073"] 之类数字的字符串数组列表,现在我想将其转换为 Double ArrayList
并计算数组列表中数字的总和.
代码如下:
私有ArrayList总计 = 新的 ArrayList<>();私有 ArrayListdemototal = new ArrayList<>();String total = itemData.getString(ParseBarcode.KEY_TOTAL);totals.add(total);/* 将 totals 数组中的所有值转换为 Double 并添加到 arraylist demototal*/for (int i = 0; i < totals.size(); i++) {最终字符串值 = totals.get(i);double total_ary = Double.parseDouble(value);demototal.add(total_ary);}String amount = ""+setArrayListElement(demototal);textViewSum.setText(amount);//设置总文本为amount//这里计算数量我们将setArrayListElement作为Double arraylist传递私人双 setArrayListElement(ArrayList inArray) {双倍量=0.0d;for (int i = 0; i < inArray.size(); i++) {数量 = 数量 + Double.valueOf((Double) inArray.get(i));}返还金额;}
这给了我 java.lang.NumberFormatException: Invalid double: "51,073"
异常.
为了更清晰,我添加了更多代码.
private void showItem(String json) {String itembarcode = "";字符串 itemdesc = "";字符串重量 = "";字符串率 = "";字符串制作 = "";字符串 netrate = "";字符串总数 = "";尝试 {JSONObject jsonObject = new JSONObject(json);JSONArray 结果 = jsonObject.getJSONArray(ParseBarcode.JSON_ARRAY);JSONObject itemData = result.getJSONObject(0);itembarcode = itemData.getString(ParseBarcode.KEY_BARCODE);itemdesc = itemData.getString(ParseBarcode.KEY_DESC);重量 = itemData.getString(ParseBarcode.KEY_WEIGHT);rate = itemData.getString(ParseBarcode.KEY_RATE);制作 = itemData.getString(ParseBarcode.KEY_MAKING);netrate = itemData.getString(ParseBarcode.KEY_NETRATE);总计 = itemData.getString(ParseBarcode.KEY_TOTAL);} catch (JSONException e) {e.printStackTrace();}//表开始TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);rowParams.setMargins(16, 0, 16, 0);TableLayout tableLayout = new TableLayout(AddInvEst.this);tableLayout.setLayoutParams(tableParams);TableRow newRow = new TableRow(AddInvEst.this);newRow.setLayoutParams(tableParams);barCode = new TextView(AddInvEst.this);barCode.setLayoutParams(rowParams);barCode.setGravity(Gravity.CENTER);itemDesc = new TextView(AddInvEst.this);itemDesc.setLayoutParams(rowParams);itemDesc.setGravity(Gravity.CENTER);weightLine = new TextView(AddInvEst.this);weightLine.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.75f));weightLine.setGravity(Gravity.CENTER);rateAmount = new EditText(AddInvEst.this);rateAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));rateAmount.setGravity(Gravity.CENTER);rateAmount.addTextChangedListener(rateTextWatcher);MakingAmount = new EditText(AddInvEst.this);MakingAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));制作Amount.setGravity(Gravity.CENTER);MakingAmount.addTextChangedListener(mkAmountTextWatcher);netRate = new TextView(AddInvEst.this);netRate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));netRate.setGravity(Gravity.CENTER);netrates.add(netrate);itemtotal = new TextView(AddInvEst.this);itemtotal.setLayoutParams(rowParams);itemtotal.setGravity(Gravity.CENTER);totals.add(total);//将totals数组中的所有值转换为Double并添加到arraylist demototalfor (int i = 0; i < totals.size(); i++) {最终字符串值 = totals.get(i);double total_ary = Double.parseDouble(value.replace(",","."));demototal.add(total_ary);}barCode.setText(itembarcode);itemDesc.setText(itemdesc);weightLine.setText(weight);rateAmount.setText(rate);制作金额.setText(制作);netRate.setText(netrate);itemtotal.setText(total);String amount = ""+setArrayListElement(demototal);textViewSum.setText(amount);//设置总文本为amountnewRow.addView(barCode);newRow.addView(itemDesc);newRow.addView(weightLine);newRow.addView(rateAmount);newRow.addView(makingAmount);newRow.addView(netRate);newRow.addView(itemtotal);itemTable.addView(newRow);}//这里计算数量我们将setArrayListElement作为Double arraylist传递私人双 setArrayListElement(ArrayList inArray) {双倍量=0.0d;for (int i = 0; i < inArray.size(); i++) {数量 = 数量 + Double.valueOf((Double) inArray.get(i));}返还金额;}
<块引用>
这是 textwatcher 和计算 netrate 和 item total 的代码.
private void calculateAndShow(double wt, double rt, double mk) {String w = weightLine.getText().toString();wt = Double.parseDouble(w);双净率 = rt + mk;double Total = (NetRate/10) * wt;netRate.setText(NetRate + "");itemtotal.setText(Total + "");}私人 TextWatcher rateTextWatcher = new TextWatcher() {@覆盖public void beforeTextChanged(CharSequence s, int start, int count, int after) {}@覆盖public void onTextChanged(CharSequence s, int start, int before, int count) {}@覆盖公共无效 afterTextChanged(可编辑的){字符串率 = rateAmount.getText().toString();rt = Double.parseDouble(rate);计算并显示(wt,rt,mk);费率.添加(费率);}};私人 TextWatcher mkAmountTextWatcher = new TextWatcher() {@覆盖public void beforeTextChanged(CharSequence s, int start, int count, int after) {}@覆盖public void onTextChanged(CharSequence s, int start, int before, int count) {}@覆盖公共无效 afterTextChanged(可编辑的){String mkAmt = MakingAmount.getText().toString();mk = Double.parseDouble(mkAmt);计算并显示(wt,rt,mk);mkCharges.add(mkAmt);}};
你不能在你的值中使用 , 来解析成双精度
试试:
double total_ary = Double.parseDouble(value.replace(",","."));
I have a string arraylist with numbers like ["51,073","51,074","51,073"] now i want to convert this into a Double ArrayList
and calculate the sum of the numbers in the array list.
Here is the code :
private ArrayList<String> totals = new ArrayList<>();
private ArrayList<Double> demototal = new ArrayList<>();
String total = itemData.getString(ParseBarcode.KEY_TOTAL);
totals.add(total);
/*converting all values from totals array to Double and add to arraylist demototal*/
for (int i = 0; i < totals.size(); i++) {
final String value = totals.get(i);
double total_ary = Double.parseDouble(value);
demototal.add(total_ary);
}
String amount = ""+setArrayListElement(demototal);
textViewSum.setText(amount);//set total text to amount
//calculate amount here we pass setArrayListElement as Double arraylist
private Double setArrayListElement(ArrayList inArray) {
Double amount = 0.0d;
for (int i = 0; i < inArray.size(); i++) {
amount = amount + Double.valueOf((Double) inArray.get(i));
}
return amount;
}
This gives me java.lang.NumberFormatException: Invalid double: "51,073"
exception.
For more clarity I am adding some more code.
private void showItem(String json) {
String itembarcode = "";
String itemdesc = "";
String weight = "";
String rate = "";
String making = "";
String netrate = "";
String total = "";
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray(ParseBarcode.JSON_ARRAY);
JSONObject itemData = result.getJSONObject(0);
itembarcode = itemData.getString(ParseBarcode.KEY_BARCODE);
itemdesc = itemData.getString(ParseBarcode.KEY_DESC);
weight = itemData.getString(ParseBarcode.KEY_WEIGHT);
rate = itemData.getString(ParseBarcode.KEY_RATE);
making = itemData.getString(ParseBarcode.KEY_MAKING);
netrate = itemData.getString(ParseBarcode.KEY_NETRATE);
total = itemData.getString(ParseBarcode.KEY_TOTAL);
} catch (JSONException e) {
e.printStackTrace();
}
//table started
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
rowParams.setMargins(16, 0, 16, 0);
TableLayout tableLayout = new TableLayout(AddInvEst.this);
tableLayout.setLayoutParams(tableParams);
TableRow newRow = new TableRow(AddInvEst.this);
newRow.setLayoutParams(tableParams);
barCode = new TextView(AddInvEst.this);
barCode.setLayoutParams(rowParams);
barCode.setGravity(Gravity.CENTER);
itemDesc = new TextView(AddInvEst.this);
itemDesc.setLayoutParams(rowParams);
itemDesc.setGravity(Gravity.CENTER);
weightLine = new TextView(AddInvEst.this);
weightLine.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.75f));
weightLine.setGravity(Gravity.CENTER);
rateAmount = new EditText(AddInvEst.this);
rateAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
rateAmount.setGravity(Gravity.CENTER);
rateAmount.addTextChangedListener(rateTextWatcher);
makingAmount = new EditText(AddInvEst.this);
makingAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
makingAmount.setGravity(Gravity.CENTER);
makingAmount.addTextChangedListener(mkAmountTextWatcher);
netRate = new TextView(AddInvEst.this);
netRate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
netRate.setGravity(Gravity.CENTER);
netrates.add(netrate);
itemtotal = new TextView(AddInvEst.this);
itemtotal.setLayoutParams(rowParams);
itemtotal.setGravity(Gravity.CENTER);
totals.add(total);
//converting all values from totals array to Double and add to arraylist demototal
for (int i = 0; i < totals.size(); i++) {
final String value = totals.get(i);
double total_ary = Double.parseDouble(value.replace(",","."));
demototal.add(total_ary);
}
barCode.setText(itembarcode);
itemDesc.setText(itemdesc);
weightLine.setText(weight);
rateAmount.setText(rate);
makingAmount.setText(making);
netRate.setText(netrate);
itemtotal.setText(total);
String amount = ""+setArrayListElement(demototal);
textViewSum.setText(amount);//set total text to amount
newRow.addView(barCode);
newRow.addView(itemDesc);
newRow.addView(weightLine);
newRow.addView(rateAmount);
newRow.addView(makingAmount);
newRow.addView(netRate);
newRow.addView(itemtotal);
itemTable.addView(newRow);
}
//calculate amount here we pass setArrayListElement as Double arraylist
private Double setArrayListElement(ArrayList inArray) {
Double amount = 0.0d;
for (int i = 0; i < inArray.size(); i++) {
amount = amount + Double.valueOf((Double) inArray.get(i));
}
return amount;
}
This is the code for textwatcher and calculating the netrate and item total.
private void calculateAndShow(double wt, double rt, double mk) {
String w = weightLine.getText().toString();
wt = Double.parseDouble(w);
double NetRate = rt + mk;
double Total = (NetRate / 10) * wt;
netRate.setText(NetRate + "");
itemtotal.setText(Total + "");
}
private TextWatcher rateTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String rate = rateAmount.getText().toString();
rt = Double.parseDouble(rate);
calculateAndShow(wt, rt, mk);
rates.add(rate);
}
};
private TextWatcher mkAmountTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String mkAmt = makingAmount.getText().toString();
mk = Double.parseDouble(mkAmt);
calculateAndShow(wt, rt, mk);
mkCharges.add(mkAmt);
}
};
You cant use a , in your value to parse to a double
Try:
double total_ary = Double.parseDouble(value.replace(",","."));
这篇关于将字符串数组列表存储到双数组列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!