Now I get it. You want to display the 5 most popular movies.
First off, uncomment and fix line 24. This is required to populate your list.
First off, this is a BIG red flag. You have two arrays, 'number' and 'freq', whose indexes must be kept in sync. This is a recipe for disaster, and in your case the disaster happens when you go to sort on 'freq'. 'freq' will be sorted but then its indicies will no longer coincide with those of 'number'.
The only way around this is to create an inner class that holds the 'number' and 'freq' as a pair. Something like:
I hate to say it but this requires some major refactoring of your code. I'll try and walk you through it.
So now we have to replace your number/freq arrays with something that makes use of the new WordFreq. We could use an array but then we'd constantly be scanning it for the matching
word. A better tool for this is a Map where the key is the 'word' (movie) and the value is a WordFreq object.
Now, as this map is populated only unique words held in the keys and their associated WordFreq objects, held as values will be kept. Maps do not allow duplicate keys.
So far so good. Now we have to sort the WordFreq object based on freq. This requires the creation of a inner Comparator class because WordFreq has no natural ordering. And while we're at it we might as well sort them in descending frequency order.
So now to sort them we can make a List out of the wordFreqMap values (which are WordFreq objects).
And lastly we have to display the sorted list, which is made a bit easier because we've added a toString() method to WordFreq.
Done.
Sorry this is so involved but I didn't see any cleaner way of dealing with your parallel array issue. I'm hoping that throwing Maps and Comparators at you isn't too much.