Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ar223ni_assign4;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.util.Scanner;
- public class CountingWords {
- public static void main(String[] args) throws FileNotFoundException {
- //Add all my scanners and the filepaths
- Scanner sc = new Scanner(System.in);
- File testFile = new File("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
- FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
- Scanner readFile = new Scanner(fileReader);
- String currentRow = "";
- int totalWords = 0;
- String words[] = null;
- boolean skipLine = false;
- /*
- TODO:
- Remove empty lines
- Remove page numbers
- Count all words
- Find separtaors : .,-/?!:; to determin starts and ends of words.
- */
- while (readFile.hasNextLine()) {
- currentRow = readFile.nextLine();
- //Use regex to remove empty lines
- currentRow = currentRow.replaceAll("(?m)^[ \t]*\r?\n", "");
- //Use regex to tell what seperats words and throw them onto a empty array.
- words = currentRow.split("[ ]");
- //Take the total amout of words and add the size of the array to it! for the total amout of words
- totalWords = totalWords + words.length;
- }
- System.out.println("Total words:" + totalWords);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement