Write a java program to get a string of words and print the numbers of each word count in descending order

Answer Posted / pradeepta kumar mishra

package interview;
import java.util.*;
import java.io.*;

class WordCount{

public static void main(String[] args) {
int count =0;
try{
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter a sentence:");
String str = br.readLine();

List<Integer> l = new ArrayList<Integer>();
for(int k=str.length();k>=1;k--)
{
if(str.charAt(k-1)==' ')
{
l.add(count);
count=-1;
}
count++;
}
l.add(count);
Comparator<Integer> comparator =
Collections.reverseOrder();
Collections.sort(l,comparator);
System.out.println("Number of each word in
desc order --"+l);
}
catch(Exception e){

}
}
}

Is This Answer Correct ?    2 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why Java is not pure Object Oriented language?

873


What are the 6 mandatory procedures for iso 9001?

736


How many decimal digits is 64 bit?

717


Explain enumeration in java?

777


What is navigable map in java?

737


What are voids?

738


When should you make a function static?

731


What does ide stand for?

709


What do you mean by platform independence of Java?

757


What is keyword auto for?

892


Can we modify the throws clause of the superclass method while overriding it in the subclass?

781


How do you reverse sort a list in java?

735


What does a boolean method return?

877


What is a condition in java?

712


How is tree Mirroring implemented?

822