Java Word Occurrence Example

This example explains you that how you can count the occurrences of each word in a file. In this example we will use the HashMap for putting and getting the values. This program takes the file name from command line which returns the number of occurrences of each word separated by a single white space. In this example I have used the java.io and java.util packages.

Java Word Occurrence Example

This example explains you that how you can count the occurrences of each word in a file. In this example we will use the HashMap for putting and getting the values. This program takes the file name from command line which returns the number of occurrences of each word separated by a single white space. In this example I have used the java.io and java.util packages.

Java Word Occurrence Example

Java Word Occurrence Example

In this example we will discuss about the how many times words are repeated in a file.

This example explains you that how you can count the occurrences of each word in a file. In this example we will use the HashMap for putting and getting the values. This program takes the file name from command line which returns the number of occurrences of each word separated by a single white space. In this example I have used the java.io and java.util packages.

Example

Here I am giving a simple example which will demonstrate you about how to count occurrences of each word in a file. In this example we will read the file name through the command line for this I have used the java.util.Scanner class then I have created object of HashMap into which we will put the elements after reading from file and then we will get these elements using java.util.Iterator.

Source Code

WordFrequencyCountTest.java

import java.io.*;
import java.util.*;
class WordFrequencyCountTest
{
public static void main( String args[])
{
System.out.println("enter the file name");
Scanner scan = new Scanner(System.in);
String fname= scan.next(); 
File f1 = new File(fname);
if(!f1.exists())
{
System.out.println("Source file doesnot exists");
System.exit(0);
}
else{
try{ 
FileReader fis = new FileReader(f1);
BufferedReader br = new BufferedReader(fis); 
String str = "";
int count=0; 
HashMap<String, Integer> map = new HashMap<String, Integer>();
HashSet<String> set = new HashSet<String>();
while((str = br.readLine()) != null)
{
String[] strArray = str.split("\\s");
count=1;
for(String token : strArray)
{ 
if(map.get(token)!=null){
count=map.get(token);
count++;
map.put((String) token, count);
count=1;
}else{
map.put((String) token, count);
//set.add(token);
}
}
}
Iterator entries = map.entrySet().iterator(); 
while(entries.hasNext())
{
Map.Entry entry = (Map.Entry) entries.next();
System.out.println(entry.getKey()+ " "+entry.getValue());
} 
fis.close(); 
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}

bipul.txt

Contents of the text file is as follows

Java Example Java Example bipul
Solution Example Solution Example J2ee bipul

Output

When you will execute the above source code then the output will be as follows :

Download Source Code