hiya please help, i am a beginner and i am trying to work out 1. how to count the number of occurrances of a specified word in a text file and 2.to count how many lines the specified word appears on. below is my code so far...
Code:public class WordCounter1 { public static void main(String[] args) throws IOException { try{ // input file BufferedReader in = new BufferedReader(new FileReader(args[0])); int count = 0; int lines = 0; String line = in.readLine().toUpperCase(); // prime the pump for "while" while (line!=null) { // Tokenizer setup StringTokenizer wordFinder = new StringTokenizer(line); while (wordFinder.hasMoreTokens()) { String token = (wordFinder.nextToken()); int n = args[1].indexOf(token); if (args[1].equalsIgnoreCase(token)) count++; if (args[1] IS ON THE LINE????? lines++; } line = in.readLine(); } System.out.println("There were " + count + " occurences"); System.out.println("The word " + args[1] + " is on " + lines + " lines"); in.close(); } catch (FileNotFoundException e) { System.out.println("File opening problem."); } } }


Reply With Quote


Bookmarks