Java Program To Count Number Of Word - Java Program Example

Here  Describe A Java Program To Count Entered Word. This Program Is Count Word From Enters Sentence .When Compile  This Program Compiler Take Input As  Santense Or Any Story By User . Compiler Show OutPut After Compile Program. Output Show As Number After Count Each Word From Entered Sentence.

Program Code

package com.androidpro.in;



import
java.util.Scanner;
import
java.util.StringTokenizer;

public class
Main {

   
public static void main(String[]Args){
        Scanner ab=
new Scanner(System.in);
       
System.out.println("Enter A Sentence For Count Word");
       
String s;
        int
k,t,i=0,max=0;
       
s=ab.nextLine();
       
System.out.println("\n The sentence is:\n\n "+s);
        int
a[]=new int[20];
       
String b[]=new String[20];
       
StringTokenizer  ob=new StringTokenizer(s);
       
System.out.println("\n_____________________________________");
       
System.out.println("Words\t\t\tNumber of Characters");
       
System.out.println("_____________________________________");
        while
(ob.hasMoreTokens())
        {
            String wd=ob.nextToken()
;
           
b[i]=wd;
            int
l=wd.length();
           
a[i]=l;
           
i++;
           
System.out.println(wd+"\t\t\t\t\t"+l);

       
}
       
for(i=0;i<(20-1);i++)
        {
           
if(a[i]>a[max])
            {
                max=i
;
           
}}
        System.
out.println("_____________________________________\n");
       
System.out.println("The Longest Word is "+b[max]);
   
}



}




Output

Enter A Sentence For Count Word
Java Is Most Popular Programming Language 

 The sentence is:

 Java Is Most Popular Programming Language 

_____________________________________
Words			Number of Characters
_____________________________________
Java					4
Is					2
Most					4
Popular					7
Programming					11
Language					8
_____________________________________

The Longest Word is Programming

Process finished with exit code 0

Post a Comment

0 Comments