Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. public void countWords()throws IOException
  2. {
  3. FileReader fr = new FileReader (file); //File reader object to read file
  4. BufferedReader br = new BufferedReader (fr); //Reads text from a character-input stream
  5. String line = br.readLine(); //Store each line as a string,so we can iterate through the text file
  6.  
  7. while (line != null) //While there is a line
  8. {
  9. String []parts = line.split(" "); // parse the strings between the spaces
  10.  
  11. for( String word : parts) //Loop through array of words using an enhanced for loop
  12. {
  13.  
  14. if(isNumeric(word)!=true && isValidWord(word)==true) // Lets check if the string is not a number and is a valid word
  15. {
  16. count++; // Count the words
  17. }
  18. }
  19.  
  20. line = br.readLine(); //Read the next line
  21. }
  22. br.close(); // Close the text file
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement