Find out words occurrences in a file

In the previous post, we have seen how to find common elements among 2 unsorted arrays. In this post, we will see how to get the count of same word occurrences in a file.

Find out words occurrences in a file:

There are few steps given below that need to be followed.

  • Create a object for file
  • Read it using Buffered Reader
  • Create a Map of String and Integer as key and value respectively
  • Keep increasing the counter if key matches more than once
  • Display keys and their values
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class FindWordsOccurances {

	public static BufferedReader bufferedReader= null;
	static String  inputLine = null;
	static String key = null;
	static int value = 0;
	public static void main(String[] args) throws FileNotFoundException {
		File file = new File("C://Personal//New folder/Sample.txt");
		bufferedReader = new BufferedReader(new FileReader(file));
		
		//Create a map for counting word occurrences 
		Map<String,Integer> CountMap = new HashMap<>();
		
		try {
			while ((inputLine = bufferedReader.readLine()) != null) {
				//create an array by splitting the line
				String[] wordsArr = inputLine.split("[ \n\t\r.,;:!?(){}]");
 
				for (int counter = 0; counter < wordsArr.length; counter++) {
					 key = wordsArr[counter].toLowerCase();  //Convert into lowercase to make it case insensitive
					if (key.length() > 0) {               // If key does not exist then assign value 1
						if (CountMap.get(key) == null) {
							CountMap.put(key, 1);
						} else {
							int value = CountMap.get(key).intValue();    // If key already exists then increase value by 1
							value++;
							CountMap.put(key, value);
						}
					}
				}
				
			}
		
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		//Create an entry set to read key and value
		for(Entry<String, Integer> s : CountMap.entrySet())
		System.out.println(s.getKey()+"-- "+s.getValue());
	}
}

Output:

is– 3
are– 2
how– 2
your– 1
doing– 1
book– 1
you– 2
house– 1
dog– 1
my– 2
this– 3
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...