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%
}
Integer division!
Your
downvalue is larger than yourupand rounding down to zero, so each time you assignaccu, you are effectively doing thisaccu = (0) * 100If you are looking for precision, you should make up and down floats instead of ints, or make a cast right before you divide.