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

What is flag in python?

833


What is a function in programming?

796


Write a program to print 15 random numbers using foreach of java 8?

759


What technique can be employed to compare two strings?

808


What is the difference between the paint() and repaint() methods?

820


What is a java string?

745


Are generics important java?

741


What are the different tags provided in jstl?

774


What is the difference between stored procedure & function?

771


Explain reverse a linked list iterative solution in java?

725


Does java trim remove newline?

748


Can an object subclass another object?

800


How to retrieve data from database in java using arraylist?

771


How many JVMs can run on a single machine and what is the Just-In-Time(JIT) compiler?

828


What do you mean by pointer value and address?

807