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
How do you write a scanner class in java?
Give an example of call be reference significance.
What is object-oriented paradigm?
What is the purpose of object oriented programming?
How does finally block differ from finalize() method?
What is the ==?
Why string is a class?
What are desktop procedures?
Which methods cannot be overridden in java?
Can a constructor be private and how are this() and super() method used with constructor?
What is role of void keyword in declaring functions?
What are peerless components in java programming?
Why synchronization is important in java?
Give us the name of the list layoutmanagers in java?
Does java allow default arguments?