programming-examples/java/Core_Java/Count total number of occurences of a String in a.java

42 lines
1.1 KiB
Java
Raw Normal View History

2019-11-15 12:59:38 +01:00
Count total number of occurences of a String in a text file
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
public class WordCounter {
public static void main(String args[]) throws Exception {
if(args.length != 1) {
System.out.println("Invalid number of arguments!");
return;
}
String sourcefile = args[0];
String searchFor = "good bye";
int searchLength=searchFor.length();
String thisLine;
try {
BufferedReader bout = new BufferedReader (new FileReader (sourcefile));
String ffline = null;
int lcnt = 0;
int searchCount = 0;
while ((ffline = bout.readLine()) != null) {
lcnt++;
for(int searchIndex=0;searchIndex<ffline.length();) {
int index=ffline.indexOf(searchFor,searchIndex);
if(index!=-1) {
System.out.println("Line number " + lcnt);
searchCount++;
searchIndex+=index+searchLength;
} else {
break;
}
}
}
System.out.println("SearchCount = "+searchCount);
} catch(Exception e) {
System.out.println(e);
}
}
}