Interview Question
Country: India
void getWordCounts(String filename) {
HashMap<String, Integer> counter = new HashMap();
HashSet<String> filter = new HashSet();
filter.add("to");
filter.add("the");
filter.add("and");
try{
Path p = Paths.get(filename);
Scanner s = new Scanner(p).useDelimiter("[ ,!?.]+");
String nextWord;
while (s.hasNext()) {
nextWord = s.next();
if(filter.contains(nextWord)) continue;
if (counter.containsKey(nextWord)) {
counter.put(nextWord, counter.get(nextWord) + 1);
}else {
counter.put(nextWord, 1);
}
}
}
catch (IOException e) {
}
for (Entry e:counter.entrySet()) {
System.out.println(e.getKey() + ":" + e.getValue());
}
}
Othello.txt Contains:
- guru sishyan April 16, 2013Welcome to bhive community. Join the community and win exiting prizes.