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

Can a source file contain more than one class declaration?

514


What is the size of int in 64-bit jvm?

552


What is the Scope of Static Variable?

732


Which variables are stored in stack?

451


Difference between string, string builder, and string buffer?

530






Define "Access specifiers" in java.

610


What is object-oriented programming?

572


What methods are used to get and set the text label displayed by a button object?

554


What is the difference between post and put?

538


How many types of interfaces are there?

578


Is java a prime method?

553


What do you mean Abstraction in java?

611


How to implement a multithreaded applet?

2204


Does every java program need a main?

538


Which of the following classes will have more memory allocated?

585