Count All Words using regex - Java Regular Expressions

Java examples for Regular Expressions:Word

Description

Count All Words using regex

Demo Code

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);
    }
}

Related Tutorials