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 reverse a string in java without using string buffer?
Can a class have an interface?
How does arraylist size increase in java?
Differentiate between run time error and syntax error.
What is the difference between C++ and Java and your preferences?
Why java is call by value?
What is ph and buffers?
what is enumset?
What do you meant by active and passive objects?
What are the different types of constructor?
Difference between current previous versions of Java?
What is string literal in java?
Why method overriding is used?
Which methods cannot be overridden in java?
What is string array?