Advertisement
dimipan80

5. Count All Words

Sep 12th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. /* Write a program to count the number of words in given sentence.
  2.  * Use any non-letter character as word separator. */
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class _05_CountAllWords {
  7.  
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         Scanner scan = new Scanner(System.in);
  11.         System.out.println("Enter your sentence on single line: ");
  12.         String inputText = scan.nextLine();
  13.  
  14.         if (!inputText.isEmpty()) {
  15.             String[] words = inputText.split("[\\W]+");
  16.             System.out.println("The number of words in given sentence is: "
  17.                     + words.length);
  18.         } else {
  19.             System.out.println("Error! - The Input Text is Empty!!!");
  20.         }
  21.     }
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement