Java - Count Occurrence of Words in a text file

Output displays several lists of the same words and counts...

Page 1 of 1

2 Replies - 20083 Views - Last Post: 10 December 2010 - 08:36 PM Rate Topic: -----

#1 joseyd77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-December 10

Java - Count Occurrence of Words in a text file

Posted 10 December 2010 - 07:05 PM

I am joseyd77

I am working on a Java Project that reads a text file from the command line and outputs an alphabetical listing of the words preceded by the occurrence count. My program compiles and runs, but it displays duplicate listings of the words and they are not in the correct order. I have searched online and reviewed my book, but I can't figure out where I am going wrong...could anyone help to put me in the right direction?

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class WordCounter {
    public static void main(String[] file) throws FileNotFoundException, IOException {
        if (file.length == 0) {
            System.out.println("Usage: java WordCounter targetfile");
            System.exit(0);
        }

        BufferedReader bufferedReader = null;
        bufferedReader = new BufferedReader(new FileReader(file[0]));
        String inputLine = null;
        TreeMap<String, Integer> map = new TreeMap<String, Integer>();


        try {
            while ((inputLine = bufferedReader.readLine()) != null) {
            String[] words = inputLine.split("[ \n\t\r.,;:!?(){}]");

                for (int wordCounter = 0; wordCounter < words.length; wordCounter++) {
                    String key = words[wordCounter].toLowerCase();
                    if (key.length() > 0) {
                        if (map.get(key) == null) {
                            map.put(key, 1);
                        }
                        else {
                            int value = map.get(key).intValue();
                            value++;
                            map.put(key, value);
                        }
                    }
                    Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
                    for (Map.Entry<String, Integer> entry : entrySet) {
                        System.out.println(entry.getValue() + "\t" + entry.getKey());
                    }

                }
            
            }
        }
        catch (IOException error) {
            System.out.println("Invalid File");  
        }
        finally {
            bufferedReader.close();


        }
    }
}


From my text file, my output should be this:
1 a
1 am
1 computes
2 file
1 i
1 in
1 is
2 my
1 number
1 of
1 program
2 test
2 that
1 the
1  this
1 to
1 total
1 using
1 verify
1 words


##
instead, my output looks like the output that I captured in the text file attached (labeled testOutput.txt) I also attached my sample text file (test.txt) for reference.

This is my first post on the DIC site, so please let me know if I did anything incorrectly.

Thanks,
josey

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Java - Count Occurrence of Words in a text file

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java - Count Occurrence of Words in a text file

Posted 10 December 2010 - 07:43 PM

Pretty late here, so I might be asleep before you reply. But to get other people started, are you doing it like this on purpose? By this, I mean is there any specific reason your choosing to use a TreeMap here? If you break your task down, it is really simple, and does not require half the stuff you are doing. Firstly, seperate the words of your file into an array like you do. Next, use a sort implementation, infact the Array's sort method should work fine for this, so you could do
Arrays.sort(words);

Now your Array is sorted in alphabetical order, all you need to do now is count the occurences of each word. This can be achieved looping the Array, perhaps using an inner loop too, outer loop goes through the words in the Array while the inner loop compares the words from the outer loop, something like
String word = "";
for (int i=0;i<words.length;i++){
   word = words[i];
   for (int j=0;i<words.length;j++){
   if (words[j+1].equals(word)){
...
...
...
}
} 


Someone else may be able to iliterate further.
Good luck
Was This Post Helpful? 2
  • +
  • -

#3 joseyd77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-December 10

Re: Java - Count Occurrence of Words in a text file

Posted 10 December 2010 - 08:36 PM

nick2price,

Thanks for your reply! I am working on updating my code with your suggestions to see if that solves it. I was utilizing a TreeMap because that is what we have been working on in my course most recently. I am still pretty new to coding (obviously ...;)...lol), so I am not very confident with identifying better ways to code. Oftentimes, I do some many changes along the way that I create a lot of code duplication. I appreciate your feedback, and I will give it a shot!

~Josey

View Postnick2price, on 10 December 2010 - 06:43 PM, said:

Pretty late here, so I might be asleep before you reply. But to get other people started, are you doing it like this on purpose? By this, I mean is there any specific reason your choosing to use a TreeMap here? If you break your task down, it is really simple, and does not require half the stuff you are doing. Firstly, seperate the words of your file into an array like you do. Next, use a sort implementation, infact the Array's sort method should work fine for this, so you could do
Arrays.sort(words);

Now your Array is sorted in alphabetical order, all you need to do now is count the occurences of each word. This can be achieved looping the Array, perhaps using an inner loop too, outer loop goes through the words in the Array while the inner loop compares the words from the outer loop, something like
String word = "";
for (int i=0;i<words.length;i++){
   word = words[i];
   for (int j=0;i<words.length;j++){
   if (words[j+1].equals(word)){
...
...
...
}
} 


Someone else may be able to iliterate further.
Good luck

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1