2017-04-09 12:56:56 +08:00
|
|
|
import java.util.Scanner;
|
2017-04-18 22:57:17 +08:00
|
|
|
|
|
|
|
/**
|
2019-02-17 20:52:00 +08:00
|
|
|
* You enter a string into this program, and it will return how many words were
|
|
|
|
* in that particular string
|
2017-04-18 22:57:17 +08:00
|
|
|
*
|
2019-02-17 20:52:00 +08:00
|
|
|
* @author Marcus
|
2017-04-18 22:57:17 +08:00
|
|
|
*/
|
2019-02-17 20:52:00 +08:00
|
|
|
public class CountWords {
|
2017-06-01 12:17:06 +08:00
|
|
|
|
2019-02-17 20:52:00 +08:00
|
|
|
public static void main(String[] args) {
|
|
|
|
Scanner input = new Scanner(System.in);
|
|
|
|
System.out.println("Enter your text: ");
|
|
|
|
String str = input.nextLine();
|
2017-06-01 07:59:11 +08:00
|
|
|
|
2019-02-17 20:52:00 +08:00
|
|
|
System.out.println("Your text has " + wordCount(str) + " word(s)");
|
|
|
|
input.close();
|
2017-06-01 07:59:11 +08:00
|
|
|
}
|
2019-02-17 20:52:00 +08:00
|
|
|
|
|
|
|
private static int wordCount(String s) {
|
|
|
|
if (s == null || s.isEmpty())
|
|
|
|
return 0;
|
|
|
|
return s.trim().split("[\\s]+").length;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|