iamaamir

Finding vowels, digits, whitespaces and consonants java 1.8

Dec 13th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6. import java.util.function.IntPredicate;
  7.  
  8. /**
  9.  *
  10.  * @author Aamir khan
  11.  */
  12. public class Count {
  13.  
  14.     private String userinpt = "";
  15.     private final List<Runnable> tasks;
  16.  
  17.     public static void main(String[] args) {
  18.         Count c = new Count();
  19.         c.takeInput();
  20.         c.start();
  21.     }
  22.  
  23.     Count() {
  24.         tasks = Arrays.asList(
  25.                 () -> countVowels(),
  26.                 () -> countCons(),
  27.                 () -> countDigits(),
  28.                 () -> countWhiteSpace()
  29.         );
  30.  
  31.     }
  32.  
  33.     /**
  34.      * take user input and turn into {
  35.      *
  36.      * @IntStream} to be used by
  37.      * {@link #count(java.util.function.IntPredicate)};
  38.     *
  39.      */
  40.     private void takeInput() {
  41.  
  42.         System.out.println("Enter your Text");
  43.         userinpt = new Scanner(System.in).useDelimiter("\\z").next();
  44.  
  45.     }
  46.  
  47.  
  48.     private void start() {
  49.         ExecutorService thread = Executors.newFixedThreadPool(tasks.size());
  50.         tasks.forEach(thread::submit);
  51.         thread.shutdown();
  52.     }
  53.  
  54.     private void countVowels() {
  55.         long numberOfVowels = count(this::isVowel);
  56.         print("Total number of Vowels: " + numberOfVowels);
  57.  
  58.     }
  59.  
  60.     private void countWhiteSpace() {
  61.         long numberOfWhiteSpaces = count(Character::isWhitespace);
  62.         print("Total number of Spaces: " + numberOfWhiteSpaces);
  63.     }
  64.  
  65.     private void countCons() {
  66.         long numberOfCons = count(e -> Character.isLetter(e) && !isVowel(e));
  67.         print("Total number of Consonants: " + numberOfCons);
  68.  
  69.     }
  70.  
  71.     private void countDigits() {
  72.         long numberOfDigits = count(Character::isDigit);
  73.         print("Total number of Digits: " + numberOfDigits);
  74.  
  75.     }
  76.  
  77.    
  78.     private long count(IntPredicate filter) {
  79.         return userinpt.toLowerCase().chars().filter(filter).count();
  80.     }
  81.    
  82.    
  83.    
  84.     //util methods
  85.    
  86.    
  87.     public boolean isVowel(int codePoint) {
  88.         //checking for lower case because the input will only in lower case
  89.         return (codePoint == 'a')
  90.                 || (codePoint == 'e')
  91.                 || (codePoint == 'i')
  92.                 || (codePoint == 'o')
  93.                 || (codePoint == 'u');
  94.  
  95.     }
  96.  
  97.     public void print(String s) {
  98.         System.out.println(s);
  99.     }
  100.  
  101.    
  102.  
  103. }
Add Comment
Please, Sign In to add comment