Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
jbtLoad.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             
            
           try {
              Scanner fileScanner = new Scanner(new File("SONGDATA.txt"));
              fileScanner.useDelimiter(",");
                  
                  while (fileScanner.hasNext())
                  {
                    String nextWord=fileScanner.next();
                    nextWord=nextWord.toLowerCase();
                    Object frequency=wordTable.getValue(nextWord);
                    
                  if (frequency==null)
                          {
                              wordTable.add(nextWord,new Integer(1));
                          }
          else
                  {
                      Integer counter=(Integer) frequency;
                     int wordFrequency=counter.intValue();
                      wordFrequency++;
                      
                      wordTable.add(nextWord, new Integer(wordFrequency));
                  }
             }
              
               }catch (FileNotFoundException ex) {
                 JOptionPane.showMessageDialog(null,"Empty File.\n\n" + ex.getMessage());  
               } 
           display();
          }   
         });
Posted
Updated 15-Apr-13 9:52am
v2
Comments
Richard C Bishop 15-Apr-13 15:53pm    
You will need to ask a question and provide more details for anyone to help you.
Sergey Alexandrovich Kryukov 15-Apr-13 16:26pm    
I can see at least one problem; please see my solution.
—SA
Richard C Bishop 15-Apr-13 16:28pm    
Nice work.
Sneh_Blose 15-Apr-13 16:04pm    
Its basically what I wrote in the subject, I have code to count the frequency of words in a textfile but somehow its not counting it properly. I'm not sure where I messed up in this code
Sergey Alexandrovich Kryukov 15-Apr-13 16:08pm    
The problem is: this is incomplete code. Word table is not declared here.
—SA

Please see my comment to the question: some important declarations are missing.

However, I think I can see at least one more or less apparent bug, which is apparent even without knowing the detail of wordTable implementation. The bug is in this line:
Java
wordTable.add(nextWord, new Integer(wordFrequency));
Most likely, this method adds a new object to wordTable. Instead, this object should have a way to retrieve existing object and increment it's integer member. Furthermore, if this is the case and if your code does not throw an exception, the wordTable object does not support uniqueness by the key, where the key should be the word (string).

Correct implementation can be based, for example on java.util.HashMap<String, Frequency>:
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html[^],
see also: http://en.wikipedia.org/wiki/Hash_map[^].

In this declaration, the type Frequency is some class with integer frequency member. The class is needed to have a reference-type object for the hash map value, so you could update it when needed.

It allows to search the object by key (String, in your case) with the computational time complexity of O(1).
(Please see http://en.wikipedia.org/wiki/Big_O_notation[^].)

When you got a new word from fileScanner, you should get the Frequency object from the hash map. If the value is no found, add a new object with the frequency of 1. If it is found, increment its value by 1. At the end, you will have the dictionary of words with respective frequencies.

—SA
 
Share this answer
 
v2
Comments
CPallini 15-Apr-13 16:32pm    
Very good answer to a question almost deserving deletion. My 5. :-)
Sergey Alexandrovich Kryukov 15-Apr-13 16:39pm    
Thank you, Carlo.

This is the illustration on the idea I expressed at the end of out discussion on deletion of questions.
That is, actually, I partially agree with you, in the following sense: it's better to approach to some formally incorrect questions individually.

Yes, this post could be formally deleted for being formulated by not as a question, and I would not blame myself much if I did so (by the way, I'll also write a comment to OP to be more careful). But I can see that OP put some non-nonsense effort in the solution but missed just a point or two. Just pointing out what was wrong should help OP to finally solve the problem. As I already said, sometimes I find and excuse to violate my own rule of deleting of the posts, this is one such case...

—SA
As others already pointed out, your question (and your code) is incomplete. Hence I may give you just a couple of remarks:
  • Are you sure the world delimiter is just a comma (usually it is a blank and other punctuation characters)?
  • Quote:
    wordTable.add(nextWord, new Integer(wordFrequency));
    this line looks suspicious. You shouldn't add, you should instead set that is overwrite previous frequency value with the new one (just a guess, as Sergey pointed out you didn't provide wordTable declaration).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Apr-13 16:50pm    
I assume you were posting it at the same time as I did; then a 5. :-)
—SA
CPallini 15-Apr-13 17:04pm    
Yes, I saw your answer just after have posted mine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900