Highest Frequency of Letter(s) in each Line of File-Core Java Programming -


stackoverflow people,

need java advice , code reads file(file.txt) , prints console list of letters occurred highest frequency in each line of file, followed frequency.

the list of letters should alphabetical list of upper case letters followed alphabetical list of lower case letters.


sample input file(file.txt)

when riding bicycle backwards down one-way street, if the
wheel falls of canoe, how many ball bearings take fill
water buffalo?
hello howard.


sample output

e 6
al 7
3
hlo 2

sample code:

public class readfilefromsystem{        public static void main(string args[]){            bufferedreader in = new bufferedreader(new filereader("c:/input.txt"));//the file containing lines.        hashmap<character,integer> lettercount = new hashmap<character, integer>();            int counter =0;              // string buffer file reading               stringbuffer str;             // reading line line file                while ((str = in.readline()) != null) {                  // process each characters                    (int = 0; < str.length(); i++) {                           char c = str.charat(i);                                            if (character.isletter(c)){                               if(counter>1){                                           counter++;                                                      }else{                                  lettercount.put(c,1);                                                       }                                           }            }            system.out.println("the alphabet"+c+" has highest frequency of "+counter);       }  

it looks have it. need initialize new map inside of while loop. and, need use inside of for loop collect data.

you using 1 counter variable. instead, need use map hold multiple counters, 1 each character.

the following psuedo code want do:

for each line in file:     initialize empty map<string, integer>     each character in line:         if map contains character:             increment count char in map         else:             add character map count of 1     iterate through map , find highest values, , print keys 

now, can translate each line of psuedo cod java, , have answer.

here, inner for loop:

lettercount = new hashmap<character, integer>(); (int = 0; < str.length(); i++) {     char c = str.charat(i);                      if (character.isletter(c)){         if(lettercount.containskey(c)){                    lettercount.get(c)++;                                  }else             lettercount.put(c,1);                                 }                           } // here find max occurances int maxcount = 0; for(integer count: lettercount.values())     if (count > maxcount) maxcount = count; // find entries had many occurances string characters = ""; (entry<character, integer> entry: lettercount.entryset()) {     if (entry.getvalue().equals(maxcount))         characters += entry.getkey(); } 

at end of this, string characters contain repeated chars in line. , maxcount contain count.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -