I am trying to count the number of occurrences of similar words in a paragraph in Java read from a file, but for some reasons the count is not working. Can you please tell me how to modify the method so that it can work.
void countsmwrd(String str) {
int count = 0;
String temp = "";
ArrayList<String> vx = new ArrayList<String>();
System.out.println("\nThe tokens are: ");
StringTokenizer s = new StringTokenizer(str, " ,.", true);
for (int i = 0; s.hasMoreTokens(); i++) {
vx.add(s.nextToken());
}
for (int i = 0; i < vx.size(); i++) {
String c = vx.get(i);
for (int j = i; j < vx.size(); j++) {
String k = vx.get(j);
if (c == k && temp.indexOf(c) == -1) {
count = count + 1;
}
}
if (temp.indexOf(c) == -1) {
temp = temp + c;
System.out.println("Character " + c + " occurs " + count + " times");
}
count = 0;
}
}