For reading the file:
File file = new File("C:/myFileHere.txt");
BufferedReader in = null;
try
{
in = new BufferedReader(new FileReader(file));
String line;
//loop through each line in the file
while ((line = in.readLine()) != null)
{
//split line up by any words separated by a space
String[] words = line.split(" ");
//get array count for total words on this line
//loop through words and keep track of unique words with a hashmap
}
}
catch (Exception e)
{}
You can use HashMap to put each word in as a key. For each word you add, check to see if it's already a key. If so, increase the value of that key. This'll keep track of how many times a word shows up. The size of the hashmap will tell you how many unique words there are and the value of each key will tell you how many times that word shows up.
After that, the rest is just simple math.
Last edited by Phaelax; Oct 11th, 2006 at 2:54 pm.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Offline 856 posts
since Mar 2004