import javabook.*;

/*
 * Introduction to OOP with Java 2nd Edition, McGraw-Hill
 *	
 *   Count the number of vowels in a given string. This
 *   version uses the toUpperCase method to convert
 *   the characters in the input string to the upper case.
 *  
 * @author Dr. Caffeine
 *
 */
class CountVowels
{
    public static void main (String[] args)
    {
        MainWindow  mainWindow;
        OutputBox 	outputBox;
        InputBox    inputBox;
        
        mainWindow  = new MainWindow("Count Words");
        outputBox   = new OutputBox( mainWindow );
        inputBox    = new InputBox( mainWindow );
                
        mainWindow.setVisible( true ); 
        outputBox.setVisible( true );
        
        //Attempt No. 2
        
        //There is still a problem with the Attempt No. 2 code. 
        //If the sentence ends with one or more blank spaces, then the 
        //value for wordCount will be one more than the actual number of 
        //words in the sentence. It is left as an exercise to correct 
        //this bug (see exercise 17 on page 408).

        int     index, wordCount, numberOfCharacters;
        
        String  sentence = inputBox.getString("Enter a sentence:");
        
        numberOfCharacters  = sentence.length( );
        index               = 0;
        wordCount           = 0;
        
        while ( index < numberOfCharacters ) {
        
            //ignore blank spaces
            while (index < numberOfCharacters && 
                   sentence.charAt(index) == ' ') {
                  
                index++;
            }
          
            //now locate the end of the word
            while (index < numberOfCharacters && 
                   sentence.charAt(index) != ' ') {
                
                index++;
            }
          
            //another word is found, so increment the counter
            wordCount++;
        
        }
        
        //display the result
        outputBox.printLine( "Input sentence: " + sentence );
        outputBox.skipLine( 1 );
        outputBox.printLine( "Word count:     " + wordCount + " words" );
            
    }
    
}