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

How many bytes are a float?

501


Who is founder of java?

524


What is finalize() function in java?

555


What is Hierarchy of exception?

643


Can java arraylist hold different types?

486






How many bits are in a sentence?

579


Is java a software?

511


How do you use compareto in java?

549


What is consumer in java?

520


What is "this" keyword in java? Explain

653


What is singleton class in java and how can we make a class singleton?

562


Can a static class have a constructor java?

544


Which is the best sorting technique in java?

515


What exactly is java?

506


Can one thread block the other thread?

599