0

i'm having trouble with a code. I have read words from a text file into a String array, removed the periods and commas. Now i need to check the number of occurrences of each word. I managed to do that as well. However, my output contains all the words in the file, and the occurrences. Like this: the 2 birds 2 are 1 going 2 north 2 north 2

Here is my code:

public static String counter(String[] wordList)
{
    //String[] noRepeatString = null ;
    //int[] countArr = null ;

    for (int i = 0; i < wordList.length; i++) 
    {
         int count = 1;
         for(int j = 0; j < wordList.length; j++)
         {
             if(i != j)  //to avoid comparing itself
             {
                 if (wordList[i].compareTo(wordList[j]) == 0)   
                 {
                     count++;
                     //noRepeatString[i] = wordList[i];
                     //countArr[i] = count;
                 }
             }
         }

         System.out.println (wordList[i] + " " + count);

     }

    return null; 

I need to figure out 1) to get the count value into an array.. 2) to delete the repetitions. As seen in the commenting, i tried to use a countArr[] and a noRepeatString[], in hopes of doing that.. but i had a NullPointerException.

Any thought on this matter will be much appreciated :)

Tsar
  • 170
  • 1
  • 6
  • 20
  • 1
    Any reason for not using collections (`List`, `Set` etc)? – fge Mar 15 '14 at 14:30
  • could you plz show us what have you tired that resulted in NullPointerException – exexzian Mar 15 '14 at 14:32
  • Still a beginner.. i'll learn those eventually. – Tsar Mar 15 '14 at 14:34
  • @sansix what did u mean? if i remove those comment markers it results in NullPointerException. – Tsar Mar 15 '14 at 14:36
  • You should start with mastering collections _before_ mastering arrays imho; arrays are a "pain" (cannot resize, need to manipulate indices, etc) compared to collections – fge Mar 15 '14 at 14:38
  • @fge i should learn them in that case, but this is for an undergraduate assignment. Have to stick to the rules of the lecturer . – Tsar Mar 15 '14 at 14:41
  • @Shifaza where are you initializing your arrays in your code – exexzian Mar 15 '14 at 15:06
  • @sansix wordList[] contains words from another method. The two arrays i have 'commented', i have initialized to null. – Tsar Mar 15 '14 at 15:13
  • @Shifaza that I saw but what I meant to say is that you haven't declared size for your array. You are just assigning to some uninitialized memory. like this `noRepeatString = new String([wordList.length])` – exexzian Mar 15 '14 at 15:31
  • @sansix What would be a better way to do it? I cant declare the array inside a loop. I cant initialize it without knowing the content.. which is why i assigned it to null. – Tsar Mar 15 '14 at 15:56

2 Answers2

1

I would first convert the array into a list because they are easier to operate on than arrays.

List<String> list = Arrays.asList(wordsList);

Then you should create a copy of that list (you'll se in a second why):

ArrayList<String> listTwo = new ArrayList<String>(list);

Now you remove all the duplicates in the second list:

HashSet hs = new HashSet();
hs.addAll(listTwo);
listTwo.clear();
listTwo.addAll(hs);

Then you loop through the second list and get the frequency of that word in the first list. But first you should create another arrayList to store the results:

ArrayList<String> results = new ArrayList<String>;
for(String word : listTwo){
int count = Collections.frequency(list, word);
String result = word +": " count;
results.add(result);
}

Finally you can output the results list:

for(String freq : results){
System.out.println(freq);}

I have not tested this code (can't do that right now). Please ask if there is a problem or it doesnÄt work. See these questions for reference:

How do I remove repeated elements from ArrayList?

One-liner to count number of occurrences of String in a String[] in Java?

How do I clone a generic List in Java?

Community
  • 1
  • 1
tomet
  • 2,416
  • 6
  • 30
  • 45
  • Is there a way to do it without list or hash? – Tsar Mar 15 '14 at 15:28
  • You probably could do it somehow but this is more elegant. Have a look at lists and hashs, it is always good to get to know new stuff because one needs it in a problem. Use this as an opportunity to learn those two things! You'll use them a lot. :) If you found this answer useful, please mark it as accepted :D – tomet Mar 15 '14 at 15:29
  • In the future i shall :) But my lecturer set the scope of this for basic stuff. Thanks anyway for the effort. Really appreciate it – Tsar Mar 15 '14 at 15:54
0

some syntax issues in your code but works fine

ArrayList<String> results = new ArrayList<String>();
for(String word : listTwo){
int count = Collections.frequency(list, word);
String result = word +": "+ count;
results.add(result);
}