Counting Words in a File

One solution for this problem:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class WordCounter {
	Map<String, Integer> words = new HashMap<String, Integer>();

	private void addWord(String word) {
		if (words.get(word.toLowerCase()) == null) {
			words.put(word.toLowerCase(), 1);
		} else {
			words.put(word.toLowerCase(), words.get(word.toLowerCase()) + 1);
		}
	}

	private void displayWords() {
		Map<String, Integer> sortedWords = new TreeMap<String, Integer>(words);
		for (String word : sortedWords.keySet()) {
			System.out.println(word + ": " + sortedWords.get(word));
		}
	}

	private String stripCharacters(String word) {
		return word.replaceAll("[\\.,]$", "");
	}

	private void parseLine(String line) {
		StringTokenizer tokenizer = new StringTokenizer(line);
		while (tokenizer.hasMoreTokens()) {
			addWord(stripCharacters(tokenizer.nextToken()));
		}
	}

	private void parseFile(String filename) {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));

			String line;
			while ((line = reader.readLine()) != null) {
				parseLine(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void countWords(String filename) {
		parseFile(filename);
		displayWords();
	}

	public static void main(String[] args) {
		new WordCounter().countWords("input.txt");
	}
}

As mentioned somewhere else, I dislike Scanner, I much prefer doing my own parsing with StringTokenizer… But I’m sure there would be much betters solutions with Scanner.

The key methods here are parseLine, which breaks up lines into words, and addWord which adds a word to the words map if it is not there yet, or increments the counter if it is.

 
---

Comment

your_ip_is_blacklisted_by sbl.spamhaus.org

---