School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Subscribe to EdwinNameless' Fake Blog        RSS Feed
-----

Counting Words in a File

Icon Leave Comment
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.

0 Comments On This Entry

 

About...

This is my fake blog. 'Cause I also have a real blog with real stuff in it.

July 2010

S M T W T F S
    123
45678910
11121314151617
18192021222324
2526272829 30 31

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)