AlexKondov

Java Word Counter

May 22nd, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.36 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class CountAllWords {
  3.  
  4.     public static void main(String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.         String input = in.nextLine();
  7.         input = input.replaceAll("\\W+", " ");
  8.         String[] words = input.split(" ");
  9.         int count = 0;
  10.        
  11.         for (String string : words) {
  12.             count++;
  13.         }
  14.         System.out.println(count);
  15.     }
  16.  
  17. }
Advertisement
Add Comment
Please, Sign In to add comment