import javax.swing.JOptionPane;
/** A class containing word-related utilities 
 *  that are all static methods (Therefore we call them with the class name
 */
public class NeatStuff {
  /** Displays message in JOptionPane indicating whether or not
   *  the String word is a palindrome.
   */
  public static void checkPalindrome(String word) {
    boolean isPalindrome = true;
    int i = 0;
    while (( i < (int) word.length() / 2) && isPalindrome){ /* check condition */
    /** as long as the condition is true the body of statements are executed. */
      if (!((word.substring(i,i+1)).equals(word.substring(word.length() 
                                         - (i+1),word.length() - (i))))) {
         isPalindrome = false;
      }
      i++;  /** if i is not incremented we may not satisfy our condition 
             *  in some cases, this could cause the program to loop endlessly. */
    }
    if (isPalindrome) { /** if the condition is true - perform statement body */
      JOptionPane.showMessageDialog(null, 
                       "This is a palindrome!!! Smart thinking! " );
    } else {           /* otherwise perform this statement body */
      JOptionPane.showMessageDialog(null, 
                       "This is not a palindrome!!! What were you thinking? " );
    }
  }
 /** Returns the number of times that the String containing a letter occurs in the
  * String containing a phrase.  Next lesson we will look at easier way to perform
  * this using a char primitive.
  */
  public static int countLetter(String phrase, String letter) {
    int i = 0;
    int count  = 0;
    while (i < phrase.length()) { /** while the condition is true execute this body */ 
      if ((phrase.substring(i, i+1)).equals(letter)) {
        count++;
      }
      i++;  /** remember to increment i or you will stay in the loop forever */
    }
    return count;
  }
  /** Returns the number of words in the phrase.  Assumes that all words are delineated
   *  by a single space.  
   */
  public static int countWords(String phrase) {
    int i = 0;
    int count  = 0;
    while (i < phrase.length()) {
      if ((phrase.substring(i, i+1)).equals(" ")) {
        count++;
      }
      i++;  /** remember to increment i or you will stay in the loop forever */
    }
    return count + 1 ; /** add 1 because there is no space before the first word */
    }
}
    
    
    
