1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.util.StringTokenizer; public class StringCount { public static void main(String[] s) { String sentence = "The Java language was designed at Sun Microsystems over a five year " + "period, culminating in its first release in January 1996"; StringCount st = new StringCount(); st.count(sentence); st.countByTokenizer(sentence); } public void count(String sentence) { String[] words = sentence.split(" "); System.out.println("Number of characters " + sentence.length()); System.out.println("Number of words " + words.length); } public void countByTokenizer(String sentence) { StringTokenizer st = new StringTokenizer(sentence); System.out.println("Number of characters " + sentence.length()); System.out.println("Number of words " + st.countTokens()); } } |