0

Currently the code below will give me a line by line listing of the values, if i wanted to sum the values for one total, can someone give me an example of going about doing this?

if (value != null && !value.isEmpty()) {
    Set set = value.keySet();
    Object[] key = set.toArray();
    Arrays.sort(key);
    // System.out.println("test" + Arrays.asList(key));

    for (int i = 0; i < key.length; i++) {
        // System.out.println(value.get((String)key[i]))
        ArrayList list = (ArrayList) value.get((String) key[i]);

        if (list != null && !list.isEmpty()) {
            Iterator iter = list.iterator();
            double itemValue = 0;

            while (iter.hasNext()) {
                Iemunbuf p = (Itemunbuf) iter.next();

                if (p != null) {
                    propertyValue = itemValue + p.getitemValue().doubleValue();
                    System.out.println(itemValue);
                }

                // buf2.append(NL);
                buf2.append(t1 + "\t");
                // System.out.println(t1);
                buf2.append(NL);
            }

            buf1.append(NL);
            double amount = itemValue;
            buf1.append("  " + money.format(amount) + "  ");
        }
    }
}
1
  • This is not your actual code. This code doesn't even compile, and when the obvious errors are fixed, it doesn't do what you imply it does. Why not show us your actual code? Commented Sep 13, 2010 at 16:53

3 Answers 3

2

Above the for loop loop, put:

double totalAmount = 0.0;

Inside the for loop, right after the double amount = itemValue line, put:

totalAmount += amount;
Sign up to request clarification or add additional context in comments.

2 Comments

It is totalling, however, i want only want the final total. Results below. this 500000.0 1000000.0 this 1500000.0 100000.0 this 1600000.0 30000.0 this 1630000.0 40000.0 this 1670000.0 200000.0 this 1870000.0 250000.0 this 2120000.0
So only record it outside of the for loop. Don't add the totalAmount to the buffer until after the for loop closes.
1

Don't know why you assign a value to amount just to print it - I suspect that what you mean to do was use amount as a grand total. Well it won't work the way you have written as amount is declared inside your loop and so will always be initialized to 0.0 before you make the assignment - that and it is a flat out assignment and not summation. Declare amount outside the loop like Erick suggested then say amount += itemValue.

Hope I understood your question properly.

Comments

0

Judging by the code you posted your value variable has the type Map<String, ArrayList<Itemunbuf>> but as you aren't making use of any generic types you may want to read up on that first (Sun tutorial).

You can simplify your code to the following

if (value != null) {
    for (ArrayList<Itemunbuf> list : value.getValues()) {
        if (list != null) {
            for (Itemunbuf p : list) {
                if (p != null) {
                    // do stuff
                }
            }
        }
    }
}

If you define your totalAmount variable outside the scope of this code and increment it where I have the // do stuff comment you will get the sum of all the Itemunbuf objects contained in your values Map

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.