您可以通过下例轻松地统计字符串中的字数:
String words = "One Two Three Four"; int countWords = words.split("\\s").length; System.out.println(countWords);
亲自试一试
教程:Java 字符串
参考手册:Java 字符串方法