1

This for loop should sum all values contained in the treemap. This works, but after the inner loop the values up and down are used to calculate the accuracy. It seems that this is never executed.

What am I doing wrong?

            // for each row
        for (Entry<String, TreeMap<String, Integer>> table_row : table.entrySet()) {

            // write the label
            String row = table_row.getKey() + ",";

            int up = 0;
            int down = 0;
            float accu = 0;
                    // for each treemap in the row
            for (Entry<String, Integer> collumn : table_row.getValue().entrySet()) {
                row += collumn.getValue() + ",";
                down += collumn.getValue();//this works
                if (collumn.getKey() == table_row.getKey()) {
                    up = collumn.getValue();
                    }
            }

    --------->  accu = (up / down) * 100; //this is not executed?? 
            System.out.println("t: " + up + " n: " + down + " a: " +  accu);
            row = row + Float.toString(accu) + " %";
            writer.println(row);//this works too but output is always 0%
        }
4
  • Then, I think statement being executed, but value might be ZERO Commented Jul 31, 2012 at 20:33
  • When you say it isn't executed, do you mean accu is an unexpected value? Commented Jul 31, 2012 at 20:34
  • @thegrinner yes accu is always 0 Commented Jul 31, 2012 at 20:35
  • Sounds like the what a couple of the answers mentioned: floating point versus integer math. Commented Jul 31, 2012 at 20:37

4 Answers 4

3

You probably don't want == in your if comparison, but rather equals()... unless you are expecting them to be the same instance of the string.

Sign up to request clarification or add additional context in comments.

4 Comments

No the keys are being compared with ==, they are strings.
Note... this would cause a zero value of up.
yes they are strings indeed, so i will change that, but the if statement is reached anyway
Reached, but are you sure that it is entered? Does 'up' have a value after the loop?
3

I assume that you are asking why accu is always 0 as in inline your comments.
accu = (up / down) * 100;
The literal 100 is an int as well as up and down. So the result probably rounds to 0

Just cast to float so that you don't lose precision.
accu = ((float)up / down) * 100;
This will work as the cast will precede the division and since if either operand if float the other also is converted to float even if integer

Comments

1

Integer division!

Your down value is larger than your up and rounding down to zero, so each time you assign accu, you are effectively doing this accu = (0) * 100

If you are looking for precision, you should make up and down floats instead of ints, or make a cast right before you divide.

Comments

0

The problem you're having is that both up and down are ints. When you divide an int by an int, the result is a int with any decimal truncated. It looks like you want a percent, so you should do something like:

accu = ((float) up / down) * 100;

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.