java hashmap word count from a text file -


i trying code read info text file, need find out how many times each word separated white space occurs. need output in alphabetical order count of each word. looking use treemap, keyset() , iterator. code incomplete , quite stuck.

    import java.util.hashmap;     import java.util.map      public class wordcount<e extends comparable<e>> {          private static map<string, integer> map = new hashmap<string, integer>();          static {             fillmap(map, "alice.txt");         }          private static void fillmap(map<string, integer> map, string filename) {          } 

}

this exact code asked for. save every word , count them. if word gets didn't exist, add map, if did, increase value. @ end, print keys , values. use it, if got question, ask.

import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.util.hashmap; import java.util.map; import java.util.treemap;  /*  * @author mr__hamid  */ public class overflow {      public static void main(string[] args) throws filenotfoundexception, ioexception {          map m1 = new hashmap();          try (bufferedreader br = new bufferedreader(new filereader("file.txt"))) {             stringbuilder sb = new stringbuilder();             string line = br.readline();              while (line != null) {                 string[] words = line.split(" ");//those words                 (int = 0; < words.length; i++) {                     if (m1.get(words[i]) == null) {                         m1.put(words[i], 1);                     } else {                         int newvalue = integer.valueof(string.valueof(m1.get(words[i])));                         newvalue++;                         m1.put(words[i], newvalue);                     }                 }                 sb.append(system.lineseparator());                 line = br.readline();             }         }         map<string, string> sorted = new treemap<string, string>(m1);         (object key : sorted.keyset()) {             system.out.println("word: " + key + "\tcounts: " + m1.get(key));         }     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -