diff options
Diffstat (limited to 'src/com/ensemble/wordcount/WordCount.java')
-rw-r--r-- | src/com/ensemble/wordcount/WordCount.java | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/com/ensemble/wordcount/WordCount.java b/src/com/ensemble/wordcount/WordCount.java new file mode 100644 index 0000000..c6a63d0 --- /dev/null +++ b/src/com/ensemble/wordcount/WordCount.java @@ -0,0 +1,103 @@ +package com.ensemble.wordcount; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * This program may be used to count the number of occurences of words in a given file. + * Words are not counted if they are contained within a larger word (eg "is" and "this") + * + * @author Joe Robinson + * + */ +public class WordCount { + + public static void main(String args[]) { + + //Need to use a concurrent hashmap as we will be modifying it as we loop through + Map<String,Integer> wordCounts = new ConcurrentHashMap<String,Integer>(); + List<String> words = new ArrayList<String>(); + WordUtil wordUtil = new WordUtil(); + String filename = ""; + + while (filename.equals("")) { + + //If an argument has been entered, use it as the filename, if not prompt for one + if (args.length > 0 && args[0] != null) { + filename = args[0]; + } else { + filename = readFilename(); + } + + try { + words = wordUtil.readWordsFromFile(filename); + + } catch (IOException e) { + System.out.println("Could not open file. Please try another."); + filename = ""; + words = new ArrayList<String>(); + } + + //Check that the file actually contains some words, if not then prompt for another + if (words.size() == 0) { + System.out.println("File does not contain any words. Please try another file."); + filename = ""; + words = new ArrayList<String>(); + } + + } + wordCounts = wordUtil.countWords(words); + + List<String> sortedWords = wordUtil.sortWords(wordCounts); + + System.out.println(); + + for (String word : sortedWords) { + + //Capitalise the first letter of the word for output + char firstChar = Character.toUpperCase(word.charAt(0)); + String outputWord = firstChar + word.substring(1); + + System.out.println(outputWord + ": " + wordCounts.get(word)); + } + } + + /** + * Prompt the user for a filename to read until one is entered, and confirm that it exists + * @return The filename, which is confirmed to exist + */ + private static String readFilename() { + + String filename = ""; + boolean fileExists = false; + while (filename == null || filename.equals("") || !fileExists) { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + try { + System.out.println("Please enter a file name to read, or type \"q\" to quit"); + filename = br.readLine(); + } catch (IOException e) { + System.out.println("Could not read input"); + System.exit(1); + } + + if (filename.equals("q")) { + System.exit(0); + } + + File file = new File(filename); + + if (file.exists()) { + fileExists = true; + } else { + System.out.println("File does not exist. Try again"); + } + } + return filename; + } +} |