I have a large text file I am reading from and I need to find out how many times some words come up. For example, the word the
. I\'m doing this line by line e
To get the number of occurrence of a specific word use the below code
Pattern pattern = Pattern.compile("Thewordyouwant");
Matcher matcher = pattern.matcher(string);
int count = 0;
while(matcher.find())
count++;
public class OccurenceOfWords {
public static void main(String args[]){
String file = "c:\\customer1.txt";
TreeMap <String ,Integer> index = new TreeMap();
String []list = null;
try( FileReader fr = new FileReader(file);//using arm jdk 7.0 feature
BufferedReader br = new BufferedReader(fr))
{
String line = br.readLine();
while(line!= null){
list = line.split("[ \n\t\r:;',.(){}]");
for(int i = 0 ; i < list.length;i++)
{
String word = list[i].toLowerCase();
if(word.length() != 0)
{
if(index.get(word)== null)
{ index.put(word,1);
}
else
{
int occur = index.get(word).intValue();
occur++;
index.put(word, occur);
}
line = br.readLine();
}
}
}}
catch(Exception ex){
System.out.println(ex.getMessage());
}
for(String item : index.keySet()){
int repeats = index.get(item).intValue();
System.out.printf("\n%10s\t%d",item,repeats);
}
}
}