Java: Count occurrences of characters in text file -
this part of program count occurrences of each letter within text file. want print a:4 b:23 c:32 , instead prints a:0b:0c:0a:0b:0c:0a:0b:0c:0 not find of occurrences of each letter. doing wrong here? help!!
             char ch = line.charat(0);                  int acounter=0;                 int bcounter=0;                 int ccounter=0;                 switch (ch)                 {                     case 'a':                        acounter++;                         break;                     case 'b':                         bcounter++;                         break;                     case 'c':                         ccounter++;                         break;                 }               bw.write ("a:" + acounter);              bw.write ("b:" + bcounter);              bw.write ("c:" + ccounter);      
char ch[] = s.tochararray(); map map = new hashmap();
    (int = 0; < ch.length; i++) {         int count = 0;         (int j = 0; j < ch.length; j++) {             if (ch[i] == ch[j])                 count++;         }         map.put(ch[i], count);      }     iterator = map.entryset().iterator();     while (it.hasnext()) {         map.entry pairs = (map.entry) it.next();         system.out.println("count of " + pairs.getkey() + " = "                 + pairs.getvalue());     }      
Comments
Post a Comment