
   import java.util.Arrays;
   import java.util.Scanner;
/*********************************************************************
 *	An	object that	contains	some text that	can be analyzed for certain
 *	characteristics.
 *	
 *	@author Arch and Nancy Harris
 *	@version	Jan 21, 2014
 *
 ********************************************************************/
   public class TextAnalyzer
   {
    /******************************************************************** 
     * theText: The object's text.
     *******************************************************************/
      private	String theText;
   
    /*********************************************************************
     * Default	constructor	that sets the text attribute to the	empty	string (NOT
     * the null pointer).
     ********************************************************************/
      public TextAnalyzer()
      {
         this.theText	= "";
      }
   
    /*********************************************************************
     * Default	constructor	that sets the text attribute to initialText.
     * @param initialText Value that this.theText is set	to
     ********************************************************************/
      public TextAnalyzer(String initialText)
      {
         this.theText	= initialText;
      }
    
    /*********************************************************************
     * Mutator	method that	updates the	text attribute	to	newText if and	only
     * if newText	is	a non-empty	string.
     * @param newText Value that	this.theText is set to
     ********************************************************************/
      public void updateText	(String newText)
      {
         if(newText != null	&&	newText.length() > 0)
            this.theText	= newText;
      }
    
    /*********************************************************************
     * Accessor method that returns	the length of the	text.
     * @return	The number of characters in the object's text
     ********************************************************************/
      public int	getLength()
      {
         return	this.theText.length();
      }
    
    /*********************************************************************
     * Accessor method that returns	the number of words in the	text.
     * A	word is defined as a	sequence	of	non-whitespace	characters 
     * separated from other words by a	sequence	of	white	space	
     * characters. The whitespace characters	are (space,	newline,	tab).	
     * @return	The number of words in the	object
     ********************************************************************/
      public int	countWords()
      {
         int count =	0;
         String word;
         Scanner textScan = new Scanner(this.theText);
         textScan.useDelimiter("[ \t\n]");
      
      //	Scanner skips all	whitespace in pulling tokens
      //	A single	word will be counted.
         while	(textScan.hasNext())
         {
            word	= textScan.next();
            if(word.length()	> 0)
            {
              // System.out.println(word); 
               count++;
            }
         }
      
         return	count; 
      }
    
    /**************************************************************
     * Accessor method that returns	the number of sentences	in	the text.
     * A	sentence	is	defined as text separated from other text	by	a sentence 
     * ending symbol. The sentence ending	symbols are	period (.),	
     * exclamation (!),	question	mark (?). Multiple ending symbols in 
     * a	row should be treated as a	single sentence end.	 
     * @return	The number of words in the	object.
     * @return
     ********************************************************************/
      public int	countSentences()
      {
         return	-1; //Did not need to be written
      }
    
    /*********************************************************************
     * Accessor method that returns	the number of times each letter appears
     * in the text. This method is case in-sensitive,	'A' and 'a'	would	be	
     * treated	the same.
     * @return	An	array	with the	count	for letters	'a' -	'z' in elements
     * 0	- 25 respectively.
     ********************************************************************/
      public int	[]	countLetters()
      {
         char candidate;
         int[] letterCount = new int[26];
        
         for	(int index = 0; index <	this.theText.length(); index++)
         {
            candidate =	this.theText.charAt(index);
            if	(Character.isLetter(candidate))
            {
               if(candidate >= 'a')
               {
               
                  letterCount[candidate -	'a']++;
               }
               else
                  letterCount[candidate -	'A']++;
            }
         }
         return	letterCount;
      }
    
    /*********************************************************************
     * Accessor method that returns	the number of times the	target
     * letter appears in this	object's	text.	This method	is	case in-sensitive, 
     * 'A' and	'a' would be treated	the same
     * @param target	The letter to search	of	in	the text.
     * @return	If	the parameter is a letter a-z	or	A-Z, return	the number 
     * of times the character	appears in the	this object's text. 
     * 
     ********************************************************************/
      public int	countLetters(char	target)
      {
      //	for simplicity, make	a copy of theText	which	is	all lower case
      //	make target	lower	case
         String someText =	this.theText.toLowerCase();
         int count =	-1;
      
         if(Character.isLetter(target))
         {
            target =	Character.toLowerCase(target);
            count	= 0;
         
         //	now find	the target
            for(int index = 0; index <	someText.length(); index++)
            {
               if	(target == someText.charAt(index))
                  count++;
            }
         }
         return	count;
      }
    
    /*********************************************************************
     * Accessor method that compares this	string to the specified	object. 
     * The result	is	true if and	only if the	argument	is	not null	and is 
     * a	TextAnalyzer object that represents	the same	sequence	of	characters 
     * as this	object. 
     * @param other The	object to compare	this object	against.
     * @return	 True	if	the objects	are equal; false otherwise.
     ********************************************************************/
      public boolean equals (TextAnalyzer other)
      {
         return	this.theText.equals(other.theText);
      }
   }
