storing a string arraylist into a double arraylist?
Asked Answered
T

4

2

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

    }
};
Topsyturvy answered 13/11, 2015 at 12:1 Comment(2)
The "," is causing the exceptionReligieuse
@Mauricio Gracia please check the updated code.Topsyturvy
I
4

You cant use a , in your value to parse to a double

Try:

double total_ary = Double.parseDouble(value.replace(",","."));
Inseparable answered 13/11, 2015 at 12:7 Comment(7)
+1 for this solution but this wont work well in every scenerios eg: it will work fine for 1100,12 but it wont work well for 11,00,12.Religieuse
@Religieuse how can 11,00,12 be a valid input considering the original question ? is that 3 number or two ? the problem has to be well definedEstus
@MauricioGracia if thats a currency value then the value can have multiple comma separators eg:if the input is taken directly from the edittext...All these are simple guess though..Religieuse
@Inseparable i tried your code and I am getting the output wrong like i am adding 51,073+51,073 i get 153.219 and I am expecting 102,146Topsyturvy
@MauricioGracia it is a currency valueTopsyturvy
@Topsyturvy You have to get seperate double values for each.eg String s="51,073"; String ss="51,073"; double d= Double.parseDouble(s.replace(",",".")); double dx= Double.parseDouble(ss.replace(",",".")); System.out.println(d+dx);Religieuse
@Religieuse I am looping on the totals array with a for loop and storing the value in a string and calling parseDouble on that string. 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); }Topsyturvy
P
2
  ArrayList<String> hello = new ArrayList<>();

    double[] bye = new double[hello.size()]; 

    for (int i = 0; i < hello.size(); ++i) { 
        bye[i] = Double.parseDouble(hello.get(i)); 
    }
Peraea answered 13/11, 2015 at 12:9 Comment(0)
L
0

Before Double.parseDouble(value) replace "," with "."

Ladino answered 13/11, 2015 at 12:6 Comment(0)
P
0

Try:

List<Double> list2 = totals.stream().map(c -> c.replace(",",".")).map(Double::parseDouble).collect(Collectors.toList());
Double sum = list2.stream().reduce((x,y)->x+y).get();

Or if you just want the sum:

Double sum = totals.stream.map(c -> c.replace(",",".")).map(Double::parseDouble).reduce((x,y)->x+y).get();
Palomino answered 13/11, 2015 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.