0


I made a program that counts the number of occurrences of all the letters in a given String using TreeMap(String, Integer) (named alphaFreq). Now I want to represent these occurrences as percentages ([A=12.3] , [B=3.0] , ....etc), so I created another TreeMap (String, Double) (named percentage), copied all keys with their values set to zero, and edited the value according to that in alphaFreq as 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 the String is all As or Bs, the result is all keys have a value of zero except A=100.0 or B=100.0 But if the String text has any other combination (say: DDJJSHHIEM) they all have the value of zero. Why? What am I doing wrong?................Thanks

4

1 回答 1

2

First thing I would do is change your calculation of percentage to:

int perc = (temp * 100)/cipherLetters.length;

Given that you are using integer division, (temp/cipherLetters.length) is probably zero.

于 2013-05-04T17:14:58.530 回答