Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. public static void main(String[] args) {
  2. String str = "The has !been divided abs on98 the issue, with 578934 784,
  3. +moderates5789 concerned @about the on: the most vulnerable.";
  4. System.out.println(countWords(str));
  5. }
  6.  
  7. public static int countWords(String input){
  8. //разбиваю строку на массив слов по пробелу
  9.  
  10. int count = 0;
  11. String[] words = input.split(" ");
  12. //Это я вывожу в консоль результаты, для наглядности...для себя
  13. //System.out.println(words.length);
  14. //System.out.println(Arrays.toString(words));
  15.  
  16. //считаю слова, в которых содержатся только буквы
  17. for (String word : words) {
  18. if (isValidWord(word)){
  19. count++;
  20. }
  21. }
  22. return count;
  23. }
  24.  
  25. private static boolean isValidWord(String word) {
  26. char[] chars = word.toCharArray();
  27. for (char c : chars) {
  28. if (!Character.isLetter(c)){
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34.  
  35. String[] words = input.split("\s*[ ,.:]\s*");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement