Write a java program to count number of unique words separated by comma (,) and their occurrence from text file.

by admin on September 3, 2014

java program to count number of unique words separated by comma (,) or newline and their occurrence from text file.

package programs;

import java.util.*;
import java.io.*;

public class uniquewrdsoccurence {

private String[] spliter;
private int[] count;
public void countWord(String Text) {

String temp1 = Text.replaceAll(“[\\n]”, ” “);
String temp = temp1.replaceAll(“,”, ” “);
spliter = temp.replaceAll(“[.?!:;/]”, “”).split(” “);
count = new int[spliter.length];
for (int i = 0; i < spliter.length; i++) {
temp = spliter[i];
for (int k = 0; k < spliter.length; k++) {
if (temp.equalsIgnoreCase(spliter[k])) {
count[k]++;
}
}
}

printResult();
}

private void printResult() {

HashMap map = new HashMap();
int counter = 0;for (int i = 0; i < spliter.length; i++) {
map.put(spliter[i].toLowerCase(), count[i]);
}

Iterator it = map.keySet().iterator();

System.out.println(“Words             Count”);
System.out.println(“#######################”);
while (it.hasNext()) {
counter++;

String temp = (String) it.next();

// prints the word 
System.out.print(temp);

// prints the spaces
for (int i = 0; i < (20 – temp.length()); i++) {
System.out.print(” “);
}

// print the value -total count
System.out.println(map.get(temp.toString()));

}
System.out.println(“#######################”);
System.out.println(“Number of unique words in file:” + counter);
}

// main method 
public static void main(String[] arg) {
String pattern = “”;
String str = null;

try {
FileInputStream filestream = new FileInputStream(
System.getProperty(“user.dir”) + “\\words.txt”);
DataInputStream datastream = new DataInputStream(filestream);
BufferedReader Br = new BufferedReader(new InputStreamReader(datastream));
while ((str = Br.readLine()) != null) {
pattern = pattern.concat(str);
pattern = pattern.concat(” “);
}
datastream.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
uniquewrdsoccurence wco = new uniquewrdsoccurence();
wco.countWord(pattern);
}

}

#SQASolutionShare on FacebookShare on Google+Share on LinkedInTweet about this on TwitterEmail this to someone

Previous post:

Next post: