SHARE
TWEET

countWords

allerost Jan 19th, 2020 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ar223ni_assign4;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.util.Scanner;
  7.  
  8. public class CountingWords {
  9.     public static void main(String[] args) throws FileNotFoundException {
  10.         //Add all my scanners and the filepaths
  11.         Scanner sc = new Scanner(System.in);
  12.         File testFile = new File("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
  13.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
  14.         Scanner readFile = new Scanner(fileReader);
  15.         String currentRow = "";
  16.         int totalWords = 0;
  17.         String words[] = null;
  18.         boolean skipLine = false;
  19.         /*
  20.         TODO:
  21.         Remove empty lines
  22.         Remove page numbers
  23.         Count all words
  24.         Find separtaors : .,-/?!:; to determin starts and ends of words.
  25.          */
  26.  
  27.         while (readFile.hasNextLine()) {
  28.             currentRow = readFile.nextLine();
  29.             //Use regex to remove empty lines
  30.             currentRow = currentRow.replaceAll("(?m)^[ \t]*\r?\n", "");
  31.             //Use regex to tell what seperats words and throw them onto a empty array.
  32.             words = currentRow.split("[ ]");
  33.             //Take the total amout of words and add the size of the array to it! for the total amout of words
  34.             totalWords = totalWords + words.length;
  35.         }
  36.         System.out.println("Total words:" + totalWords);
  37.     }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top