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 :)

10
  • 1
    Any reason for not using collections (List, Set etc)? Commented Mar 15, 2014 at 14:30
  • could you plz show us what have you tired that resulted in NullPointerException Commented Mar 15, 2014 at 14:32
  • Still a beginner.. i'll learn those eventually. Commented Mar 15, 2014 at 14:34
  • @sansix what did u mean? if i remove those comment markers it results in NullPointerException. Commented Mar 15, 2014 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 Commented Mar 15, 2014 at 14:38

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to do it without list or hash?
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
In the future i shall :) But my lecturer set the scope of this for basic stuff. Thanks anyway for the effort. Really appreciate it
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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.