Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.function.IntPredicate;
- /**
- *
- * @author Aamir khan
- */
- public class Count {
- private String userinpt = "";
- private final List<Runnable> tasks;
- public static void main(String[] args) {
- Count c = new Count();
- c.takeInput();
- c.start();
- }
- Count() {
- tasks = Arrays.asList(
- () -> countVowels(),
- () -> countCons(),
- () -> countDigits(),
- () -> countWhiteSpace()
- );
- }
- /**
- * take user input and turn into {
- *
- * @IntStream} to be used by
- * {@link #count(java.util.function.IntPredicate)};
- *
- */
- private void takeInput() {
- System.out.println("Enter your Text");
- userinpt = new Scanner(System.in).useDelimiter("\\z").next();
- }
- private void start() {
- ExecutorService thread = Executors.newFixedThreadPool(tasks.size());
- tasks.forEach(thread::submit);
- thread.shutdown();
- }
- private void countVowels() {
- long numberOfVowels = count(this::isVowel);
- print("Total number of Vowels: " + numberOfVowels);
- }
- private void countWhiteSpace() {
- long numberOfWhiteSpaces = count(Character::isWhitespace);
- print("Total number of Spaces: " + numberOfWhiteSpaces);
- }
- private void countCons() {
- long numberOfCons = count(e -> Character.isLetter(e) && !isVowel(e));
- print("Total number of Consonants: " + numberOfCons);
- }
- private void countDigits() {
- long numberOfDigits = count(Character::isDigit);
- print("Total number of Digits: " + numberOfDigits);
- }
- private long count(IntPredicate filter) {
- return userinpt.toLowerCase().chars().filter(filter).count();
- }
- //util methods
- public boolean isVowel(int codePoint) {
- //checking for lower case because the input will only in lower case
- return (codePoint == 'a')
- || (codePoint == 'e')
- || (codePoint == 'i')
- || (codePoint == 'o')
- || (codePoint == 'u');
- }
- public void print(String s) {
- System.out.println(s);
- }
- }
Add Comment
Please, Sign In to add comment