Java examples for Regular Expressions:Word
Count All Words using regex
import java.util.Scanner; public class CountAllWords { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String[] input = scn.nextLine().split("\\W+"); int counter = 0; for (int i = 0; i < input.length; i++) { counter++;/*w w w . j a v a 2 s .co m*/ } System.out.println(counter); } }