java - Calculating percentage of a map values -
made program counts number of occurrences of letters in given string using treemap(string, integer) (named alphafreq). want represent these occurrences percentages ([a=12.3] , [b=3.0] , ....etc), created treemap (string, double) (named percentage), copied keys values set zero, , edited value according in alphafreq follows:
for(int i=0; i<26; i++){ int temp; character current = (char)(i+65); string frog = current.tostring(); temp = alphafreq.get(frog); int perc = (temp/cipherletters.length)*100; system.out.println(temp+"/"+cipherletters.length+" = "+perc); percentage.put(frog,(double)perc); }
if string or bs, result keys have value of 0 except a=100.0 or b=100.0 if string text has other combination (say: ddjjshhiem) have value of zero. why? doing wrong?................thanks
first thing change calculation of percentage to:
int perc = (temp * 100)/cipherletters.length;
given using integer division, (temp
/cipherletters.length
) zero
.
Comments
Post a Comment