本文介绍了存储一个字符串数组列表变成一个双数组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串数组列表中包含的 [51073,51074,51073] 现在我想将其转换成双的ArrayList $号C $ C>和计算数组列表中的数的和
下面是code:
私人的ArrayList<字符串>总访问量=新的ArrayList<>();
私人的ArrayList<双> demototal =新的ArrayList<>();
字符串总= itemData.getString(ParseBar code.KEY_TOTAL);
totals.add(总);
/ *转换所有值总计数组翻一番,增加ArrayList的demototal * /
的for(int i = 0; I< totals.size();我++){
最终的字符串值= totals.get(我);
双total_ary = Double.parseDouble(值);
demototal.add(total_ary);
}
字符串金额=+ setArrayListElement(demototal);
textViewSum.setText(金额); //设置总文本达
//计算量下面我们通过setArrayListElement为双数组列表
私人双setArrayListElement(ArrayList中inArray){
双量= 0.0D;
的for(int i = 0; I< inArray.size();我++){
金额=量+ Double.valueOf((双人间)inArray.get(一));
}
返回量;
}
这给了我 java.lang.NumberFormatException:无效的双:51073
除了
私人无效showItem(JSON字符串){
字符串itembar code =;
字符串itemdesc =;
串重量=;
字符串率=;
字符串决策=;
字符串netrate =;
字符串总=;
尝试 {
的JSONObject的JSONObject =新的JSONObject(JSON);
JSONArray结果= jsonObject.getJSONArray(ParseBar code.JSON_ARRAY);
的JSONObject的ItemData = result.getJSONObject(0);
itembar code = itemData.getString(ParseBar code.KEY_BAR code);
itemdesc = itemData.getString(ParseBar code.KEY_DESC);
体重= itemData.getString(ParseBar code.KEY_WEIGHT);
率= itemData.getString(ParseBar code.KEY_RATE);
制作= itemData.getString(ParseBar code.KEY_MAKING);
netrate = itemData.getString(ParseBar code.KEY_NETRATE);
总= itemData.getString(ParseBar code.KEY_TOTAL);
}赶上(JSONException E){
e.printStackTrace();
}
//表开始
TableLayout.LayoutParams tableParams =新TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams =新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,1F);
rowParams.setMargins(16,0,16,0);
TableLayout tableLayout =新TableLayout(AddInvEst.this);
tableLayout.setLayoutParams(tableParams);
的TableRow NEWROW =新的TableRow(AddInvEst.this);
newRow.setLayoutParams(tableParams);
酒吧code =新的TextView(AddInvEst.this);
酒吧code.setLayoutParams(rowParams);
酒吧code.setGravity(Gravity.CENTER);
itemDesc =新的TextView(AddInvEst.this);
itemDesc.setLayoutParams(rowParams);
itemDesc.setGravity(Gravity.CENTER);
weightLine =新的TextView(AddInvEst.this);
weightLine.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.75f));
weightLine.setGravity(Gravity.CENTER);
rateAmount =新的EditText(AddInvEst.this);
rateAmount.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.5F));
rateAmount.setGravity(Gravity.CENTER);
rateAmount.addTextChangedListener(rateTextWatcher);
makingAmount =新的EditText(AddInvEst.this);
makingAmount.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.5F));
makingAmount.setGravity(Gravity.CENTER);
makingAmount.addTextChangedListener(mkAmountTextWatcher);
netRate =新的TextView(AddInvEst.this);
netRate.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.5F));
netRate.setGravity(Gravity.CENTER);
netrates.add(netrate);
itemtotal =新的TextView(AddInvEst.this);
itemtotal.setLayoutParams(rowParams);
itemtotal.setGravity(Gravity.CENTER);
totals.add(总);
//将来自总额阵列中的所有值加倍,并添加到ArrayList的demototal
的for(int i = 0; I< totals.size();我++){
最终的字符串值= totals.get(我);
双total_ary = Double.parseDouble(value.replace(,,。));
demototal.add(total_ary);
}
酒吧code.setText(itembar code);
itemDesc.setText(itemdesc);
weightLine.setText(重量);
rateAmount.setText(率);
makingAmount.setText(制作);
netRate.setText(netrate);
itemtotal.setText(总);
字符串金额=+ setArrayListElement(demototal);
textViewSum.setText(金额); //设置总文本达
newRow.addView(酒吧code);
newRow.addView(itemDesc);
newRow.addView(weightLine);
newRow.addView(rateAmount);
newRow.addView(makingAmount);
newRow.addView(netRate);
newRow.addView(itemtotal);
itemTable.addView(NEWROW);
}
//计算量下面我们通过setArrayListElement为双数组列表
私人双setArrayListElement(ArrayList中inArray){
双量= 0.0D;
的for(int i = 0; I< inArray.size();我++){
金额=量+ Double.valueOf((双人间)inArray.get(一));
}
返回量;
}
私人无效calculateAndShow(双重量,双RT,双MK){
。字符串W = weightLine.getText()的toString();
重量= Double.parseDouble(瓦特);
双NetRate = RT + MK;
双总=(NetRate / 10)*重量;
netRate.setText(NetRate +);
itemtotal.setText(共+);
}
私人TextWatcher rateTextWatcher =新TextWatcher(){
@覆盖
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){
}
@覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
}
@覆盖
公共无效afterTextChanged(编辑S){
字符串率= rateAmount.getText()的toString()。
RT = Double.parseDouble(率);
calculateAndShow(重量,RT,MK);
rates.add(率);
}
};
私人TextWatcher mkAmountTextWatcher =新TextWatcher(){
@覆盖
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){
}
@覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
}
@覆盖
公共无效afterTextChanged(编辑S){
字符串mkAmt = makingAmount.getText()的toString()。
MK = Double.parseDouble(mkAmt);
calculateAndShow(重量,RT,MK);
mkCharges.add(mkAmt);
}
};
解决方案
您不能使用,在你的价值解析为双
尝试:
双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.
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;
}
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(",","."));
这篇关于存储一个字符串数组列表变成一个双数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!