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
Is java map thread safe?
What is the difference between path and classpath variables?
Can we make the abstract methods static in java?
What is variable explain with example?
Is char a data type in java?
Does isempty check for null?
Is there any sort function in java?
Can an abstract class be a final class?
Is singleton thread safe in java?
What is the difference between access specifiers and access modifiers in java? 16
What is the full name of java?
What are the data types supported by java?
Describe how to implement singleton design pattern in struts.
What are the drawbacks of singleton class?
Explain about map interface in java?