简体   繁体   中英

Sum of numerous String Values Using Loop

enter image description here I have a String named as Price. I take values of Price from the firebase database. I want to make sum of these String Values and make a total Price. The Value of Price increases automatically according to the child in the database. For example the values are 100, 70, 50, 20, 40 etc. And it will increase accordingly.

I think there should be a loop to make the sum of all these values to make it total price.

This is what i have done so far.

 int pos = 0;                                     
 JSONObject jsonObject = new JSONObject();
 if (jsonObject.has(Integer.toString(pos))) {
 } else {                                       
     try {                                               
         jsonObject.put(Integer.toString(pos),Price);                                         
     } catch (JSONException e) {                                           
         e.printStackTrace();                                         
     }                                    
 }

 for (int i = 0; i < jsonObj.length(); i++) {
     try {                                  
         String itemInArray = jsonObj.getString(String.valueOf(i));
         int Sum =  0;
         int totalPrice = sum + itemInArray;

  } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }

But The code is not working. Please any help would be appreciated.

Let's isolate the part of the code where is the problem of Summing numerous String Values Using Loop .

for (int i = 0; i < jsonObj.length(); i++) {
     try {                                  
       String itemInArray = jsonObj.getString(String.valueOf(i));
       int Sum =  0;
       int totalPrice = sum + itemInArray;

     } catch (JSONException e) {
      e.printStackTrace();
    }

  }

Every time you loop through this code you reassign the value of 0 to Sum and as a result totalPrice s value is always 0 plus the itemInArray (which, another problem, is a String and cannot be directly added to an integer).

Assuming everything else is right and itemInArray holds a numeric value here is the code to find the total price:

try{
       int sum = 0; // you don't need the totalPrice variable, they will hold the same result 
       for (int i=0;i<jsonObj.length(); i++) {
        String itemInArray = jsonObj.getString(String.valueOf(i));
        int itemPrice = Integer.ParseInt(itemInArray);
        sum = sum + itemPrice;
      }
    }catch (JSONException e) {
      e.printStackTrace();
    }

Now the sum is not redefined in every loop, it keeps its value from the previous loop and it adds itself and itemPrice in the next iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM