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
what do you mean by marker interface in java?
what is deadlock? : Java thread
what is thread? What are the high-level thread states? : Java thread
How do you write methodology?
What are the different conditional statements?
What is the difference between static (class) method and instance method?
What is variable and its types?
What is a qualifier in a sentence?
What is constructor chaining and how is it achieved in java?
What is a programming object?
take an array with -ve and +ve value both.find out the nearest value of 0(zero).if two values are same like(-2 and +2)then extract +2 is nearest of 0(zero).
how to create multithreaded program? Explain different ways of using thread? : Java thread
Which list is sorted in java?
What are identifiers in java?
What is a key in java?