First: you did a good job on that - excellent progress. Just one problem with the sort:
The Map.Entry class does not implement Comparator, so it has no default or "natural" sort order. That's why you need to create your own Comparator for the sort. The code you found on the web is a good place to start - it defines a Comparator that compares Map.Entry objects by ignoring their keys and comparing their values.

List<Map.Entry<String, Integer>> entries = new LinkedList<Map.Entry<String, Integer>>(wordCount.entrySet());
        Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> t, Map.Entry<String, Integer> t1) {
                return t1.getValue().compareTo(t.getValue());

            }
        });

        System.out.println(entries);

Has yielded me an excellent result hope everything is going good here is my output

Enter the file path to analyse:
/home/dinesh/Desktop
[hello=3, dine=3, how=3, where=2, dinesh=1, are=1, norm=1, you=1]

still i need to make my code to prompt the user to enter an integer value such that if user enters 2 then the first two words has to be shown(based on count)for example here its hello:3,dine=3 i think i need to use an for loop for this am i right James?

Yes, a for loop is a good solution, or you could use List's subList method...

wordMap.put(word, wordMap.get(word)++)

That's quite a neat NPE generator if you start with an empty map.
Check out this from Java 8:

    wordMap.put(word, wordMap.getOrDefault(word, 0)++)

and pre-incrementing may be closer to what you areally want

    wordMap.put(word, ++ wordMap.getOrDefault(word, 0))

;)

Yes, all true. But it was just being a bit facetious, anyway. ;-)

I do that sort of thing, but, of course, with a contains call first, and prefix.

I used the following code

System.out.println("Enter an value for k to show the top k words:");
        Scanner scan1 = new Scanner(System.in);
        int k = scan1.nextInt();
        List<Map.Entry<String,Integer>>topList=new LinkedList<Map.Entry<String,Integer>>();
        topList=entries.subList(0, k);
        System.out.println(topList);

and now everything is complete gonna start my next assignment and an sincere thanks to James who has helped me alot to learn and complete my assignment.

Excellent! You've done a lot of work there, and showed that you can learn fast. You're going to ace this course.

One tiny hint: if you're using Java 7 (like you should) then you can omit many of those type specs, eg

List<Map.Entry<String,Integer>>topList=new LinkedList<>();

Java 7 will infer the missing types based on the variable's declaration.

Anyway, if we're all done with this please mark this "solved" - it's gone on long enough, and you can always start another thread for your next assignment.

It's been fun
J

Hi Dinesh 9 .. Could you please add the final code snippet of solution..for refrence..I have some problems..Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.