Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to count the number of words in given sentence.
- * Use any non-letter character as word separator. */
- import java.util.Scanner;
- public class _05_CountAllWords {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter your sentence on single line: ");
- String inputText = scan.nextLine();
- if (!inputText.isEmpty()) {
- String[] words = inputText.split("[\\W]+");
- System.out.println("The number of words in given sentence is: "
- + words.length);
- } else {
- System.out.println("Error! - The Input Text is Empty!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement