hi,
I've worked on doing an application to read a txt file, then print back the number of characters, words and the number of lines in the text file. Once thats been done the application must then be able to output the number of occurences of each word in the text file and on how many lines each word is:
e.g
the quick brow fox jumped over the
lazy dog
This would give:
2 occurences of "the"
1 line contains "the"
I'm new to java and i've managed to get the number of lines to report back but i'm not sure on how to get the rest done. I've explored using the StringTokenizer method and String.indexOf method to achieve my results but i'm finding it very difficult to code. If anyone has a chance to take a look here's the code i've been able to do:
Thanks if anyone can help me with this it's been annoying me now for a long time.Code:import java.io.*; import java.util.*; class WordCounter { // Main public static void main (String[] args) { WordCounter t = new WordCounter(); t.fileRead(); } // Read the file and output void fileRead() { String record = null; int numLines = 0; int numWords = 0; int numChars = 0; try { FileReader fr = new FileReader("test.txt"); BufferedReader br = new BufferedReader(fr); record = new String(); while ((record = br.readLine()) != null) { numLines++; } // Output values System.out.println("Number of lines:" + numLines); System.out.println("Number of words:" + numWords); System.out.println("Number of chars:" + numChars); } catch (IOException e) { // Catch possible io errors from readLine() System.out.println("IOException error!"); e.printStackTrace(); } } // End of fileRead } // End of class
Chris.


Reply With Quote


Bookmarks