package ru.apermyakov.threads;

/**
 * Class for modulate threads to find words and spaced in text.
 *
 * @author apermyakov 14.11.2017
 * @version 1.0
 * @since 14.11.2017
 */
public class WordsASpaces {

    /**
     * Private class for search words in text with ignore wrong spaces.
     *
     * @author apermyakov
     * @version 1.0
     * @since 14.11.2017
     */
    private static class Words implements Runnable {

        /**
         * Field for contain string text.
         */
        private String string;

        /**
         * Design words object.
         *
         * @param string insert text
         */
        Words(String string) {
            this.string = string;
        }

        /**
         * Method for modulate run threads action to find words.
         */
        @Override
        public void run() {
            int counter = 0;
            int spaceBeforeWord;
            int spaceAfterWord = 0;

            for (int index = 0; index < string.length(); index++) {
                if (string.charAt(index) == ' ') {
                    spaceBeforeWord = index;
                    if (spaceBeforeWord - spaceAfterWord > 1) {
                            counter++;
                    }
                    spaceAfterWord = index;
                }
            }

            if (string.length() - spaceAfterWord > 1) {
                counter++;
            }
            System.out.println(String.format("%s words.", counter));
        }
    }

    /**
     * Private class for search spaces in text.
     *
     * @author apermyakov
     * @version 1.0
     * @since 15.11.2017
     */
    private static class Spaces implements Runnable {

        /**
         * Field for contain string text.
         */
        private String string;

        /**
         * Design spaces object.
         *
         * @param string insert text
         */
        Spaces(String string) {
            this.string = string;
        }

        /**
         * Method for modulate thread run action to find spaces.
         */
        @Override
        public void run() {
            int counter = 0;

            for (char symbol : string.toCharArray()) {
                if (symbol == ' ') {
                    counter++;
                }
            }

            System.out.println(String.format("%s spaces.", counter));
        }
    }

    /**
     * Main thread of app.
     *
     * @param args args
     */
    public static void main(String[] args) {
        calculate("Hi, Petr, I work fine!");
        calculate("Hi, Petr, bye!");
        calculate("   Hi,   Petr, threads   is awesome idea!   ");
    }

    /**
     * Method for initialize threads running.
     *
     * @param string insert text
     */
    private static void calculate(String string) {
        new Thread(new Words(string)).start();
        new Thread(new Spaces(string)).start();
    }
}

@@ -2384,506 +2384,1677 @@
   * 
-Main thread of app.%0A     *%0A     * @param args args%0A     */%0A    public static void main(String%5B%5D args) %7B%0A        calculate(%22Hi, Petr, I work fine!%22);%0A        calculate(%22Hi, Petr, bye!%22);%0A        calculate(%22   Hi,   Petr, threads   is awesome idea!   %22);%0A    %7D%0A%0A    /**%0A     * Method for initialize threads running.%0A     *%0A     * @param string insert text%0A     */%0A    private static void calculate(String string) %7B%0A        new Thread(new Words(string)).start();%0A        new Thread(new Spaces(string))
+Private class for interact with user at the beginning and end of app.%0A     *%0A     * @author apermyakov%0A     * @version 1.0%0A     * @since 15.11.2017%0A     */%0A    private static class Receptionist implements Runnable %7B%0A%0A        /**%0A         * Field for phrase to user.%0A         */%0A        private String string;%0A%0A        /**%0A         * Design receptionist.%0A         *%0A         * @param string output text%0A         */%0A        Receptionist(String string) %7B%0A            this.string = string;%0A        %7D%0A%0A        /**%0A         * Method for output phrase for user.%0A         */%0A        @Override%0A        public void run() %7B%0A            System.out.println(string);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Main thread of app.%0A     *%0A     * @param args args%0A     */%0A    public static void main(String%5B%5D args) %7B%0A        calculate(%22Hi, Petr, I work fine!%22);%0A        calculate(%22Hi, Petr, bye!%22);%0A        calculate(%22   Hi,   Petr, threads   is awesome idea!   %22);%0A    %7D%0A%0A    /**%0A     * Method for initialize threads running.%0A     *%0A     * @param string insert text%0A     */%0A    private static void calculate(String string) %7B%0A        Thread hello = new Thread(new Receptionist(%22Hello! Program starts up..%22));%0A        Thread bye = new Thread(new Receptionist(%22Program is finished its work. Bye!%22));%0A        Thread findWords = new Thread(new Words(string));%0A        Thread findSpaces = new Thread(new Spaces(string));%0A%0A        hello.start();%0A%0A        try %7B%0A            hello.join();%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        findWords.start();%0A        findSpaces.start();%0A%0A        while (findWords.isAlive() %7C%7C findSpaces.isAlive());%0A%0A        bye
 .sta

Task 1017