aspose file tools*
The moose likes Beginning Java and the fly likes adding it all up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding it all up" Watch "adding it all up" New topic
Author

adding it all up

Shrey Puranik
Greenhorn

Joined: Jan 05, 2009
Posts: 8
Hello,
I have written some code, with an arraylist order and I have stored some doubles in there.

How can I output a total of all the doubles stored in the arraylist?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Welcome to JavaRanch

By doubles, I presume you mean Doubles? You can easily iterate through the List with a for, for-each or iterator, and add the members. And remember
  • Sum = 0 + a + b + c
  • Product = 1 * a * b * c
  • Shrey Puranik
    Greenhorn

    Joined: Jan 05, 2009
    Posts: 8
    Campbell Ritchie wrote:Welcome to JavaRanch

    By doubles, I presume you mean Doubles? You can easily iterate through the List with a for, for-each or iterator, and add the members. And remember
  • Sum = 0 + a + b + c
  • Product = 1 * a * b * c


  • The problem here is that new data is being added to the arrayList all the time-the total function is mainly so atthe end the user can find out wha tthe total is.

    Himanshu Gupta
    Ranch Hand

    Joined: Aug 18, 2008
    Posts: 598

    Do you want to say that in your ArrayList insertions are happening all the time and you may want to now the total at any instance of time? Well you may simply call the method which adds all the elements present in the ArrayList. If you mean something else then please write here in detail.


    My Blog SCJP 5 SCWCD 5
    Shrey Puranik
    Greenhorn

    Joined: Jan 05, 2009
    Posts: 8
    Himanshu Gupta wrote:Do you want to say that in your ArrayList insertions are happening all the time and you may want to now the total at any instance of time? Well you may simply call the method which adds all the elements present in the ArrayList. If you mean something else then please write here in detail.


    Gotcha-thanks :-)
    Shrey Puranik
    Greenhorn

    Joined: Jan 05, 2009
    Posts: 8
    Shrey Puranik wrote:
    Himanshu Gupta wrote:Do you want to say that in your ArrayList insertions are happening all the time and you may want to now the total at any instance of time? Well you may simply call the method which adds all the elements present in the ArrayList. If you mean something else then please write here in detail.


    Gotcha-thanks :-)


    It appears I was wong.

    my code is:


    int totalCost = 0;
    for (int i=0:i = order.size-1; i++){
    totalCost = totalCost + ols.tot);
    }


    any help would be appreciated.
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9293
        
      17

    Shrey Puranik wrote:
    It appears I was wong.

    my code is:


    int totalCost = 0;
    for (int i=0:i = order.size-1; i++){
    totalCost = totalCost + ols.tot);
    }


    any help would be appreciated.


    I think your code has many errors. First of all you didn't tell that is the order ArrayList typed or not. I am assuming that it is not. So this would be the code



    SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
    Shrey Puranik
    Greenhorn

    Joined: Jan 05, 2009
    Posts: 8
    Ankit Garg wrote:
    Shrey Puranik wrote:
    It appears I was wong.

    my code is:


    int totalCost = 0;
    for (int i=0:i = order.size-1; i++){
    totalCost = totalCost + ols.tot);
    }


    any help would be appreciated.


    I think your code has many errors. First of all you didn't tell that is the order ArrayList typed or not. I am assuming that it is not. So this would be the code



    by typed what do you mean? basically by clicking a button, the cuirrent element is added to the arraylist. ...
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9293
        
      17

    By typed I mean this

    ArrayList<Double> order = ...

    This would remove the unnecessary casts from the code. As per your need, you must execute the totaling code each time a new element is added into the list...
    Shrey Puranik
    Greenhorn

    Joined: Jan 05, 2009
    Posts: 8
    Ankit Garg wrote:By typed I mean this

    ArrayList<Double> order = ...

    This would remove the unnecessary casts from the code. As per your need, you must execute the totaling code each time a new element is added into the list...


    I have put:

    ArrayList order = new ArrayList();

    at the top of my class.
    Himanshu Gupta
    Ranch Hand

    Joined: Aug 18, 2008
    Posts: 598

    Ankit Garg wrote:
    Shrey Puranik wrote:
    It appears I was wong.

    my code is:


    int totalCost = 0;
    for (int i=0:i = order.size-1; i++){
    totalCost = totalCost + ols.tot);
    }


    any help would be appreciated.


    I think your code has many errors. First of all you didn't tell that is the order ArrayList typed or not. I am assuming that it is not. So this would be the code



    Ankit it seems that you mean this


    if order is an Object of ArrayList.

    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9293
        
      17

    Yes! Sorry I made a mistake in the code :P . I meant this

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: adding it all up