• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding Top 5 in Descending Order  RSS feed

 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble finding a way to display the top 5 results in descending order from this code. Any help would be appreciated.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch You cannot usually sort out that sort of thing in code, but you would need to work out the algorithm with pencil and paper first. Start by working out how to go through the collection and find the largest item. That shoul‍d be easy enough. Then try the next largest. What are you going to do if you have two large items the same size?

Don't use CAPITAL_LETTERS and underscores in Java® variable names. Use camelCase instead.
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is it possible to accomplish want I want by manipulating this code? This is a school assignment and I am kind of at a loss.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wouldn't. That code is pretty bad. There is one single method that does everything, which makes it hard to adjust.  it's impossible to change one part without it affecting the entire thing.
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, I have to use this code for the assignment. Any ideas?
 
Paul Clapham
Sheriff
Posts: 22827
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Johns wrote:Unfortunately, I have to use this code for the assignment. Any ideas?


But that code doesn't find the top 5 numbers. So you can't use that code to find the top 5 numbers.

So presumably you have to modify it in some way so that the modified version finds the top 5 numbers? If that's the case then I don't see any barrier to cleaning up the code to make it usable, as the others have already suggested.
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's were I'm stuck. I can't figure out what I need to modify and add to make the code display the top 5 results in descending order.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Johns wrote:. . . modify and add . . .
I told you yesterday that you probably can't modify that code. You need separate methods to read the numbers, to find the largest, etc. Unless you have specifically been given that code and told to modify it, you will not be able to use that code again.
Please be careful about // comments They are only intended for short comments, and they shou‍ld not be used for something which is already obvious from reading the code. Look at line 30 shou‍ld be a /* comment */ if you are keeping the comment at all
.Why are you looping a List to print it out? Have you tried this?
System.out.println(myList);
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been given this code and specifically told to modify it. That is why I am stuck on what I need to add to make this existing code print the top five results in descending order.
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this is not my code so I am not sure why it was written the way it was.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Johns wrote:I am having trouble finding a way to display the top 5 results in descending order from this code.

Top 5 unique numbers or just the top 5 numbers?
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just the top 5 in descending order.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, top 5 "numbers".
Your "numbers" are actually Strings. So do you want them descending in String order or do you want to convert them to an int (or Integer) and sort by that?
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I want it to sort by string order.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use Collections.sort(number_list) which gives you the list in ascending order and then go to the end and count backwards for 5 indexes.
You could use Collections.sort(number_list,myComparator) which you'd have to write a comparator that gives you the list in descending order and look at the first 5 indexes.
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this now but it is saying "wordsArray cannot be resolved to a variable in lines 44, 56, and 65.


       
       
        
        


 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You've defined wordsArray inside the while() loop. It will not be visible outside of the loop.
"wordsArray" is a poor name as it is not an array.
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any advice on what needs to be changed around? I am stuck again at this point and my deadline is soon. I have been messing around with this thing for days.
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this now and when I run it nothing displays. Any ideas?


       
       
        
        


 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See comments
 
Tim Johns
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have now and nothing is displaying still. Any ideas? The scanner document is actually completely full of words so I need the program to display the top 5 words and their frequence in the console.



       
       
        
        

 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.





 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!