title: Java Loops -- Strings of First and last characters of every word
body: <p>I am currently in an Intro to Programming class at San Jose and as part of our assignment we were to create a class with methods that either return a string with the first letters of each word or the last letters of each word. </p>
  
  <p>The instance variable "phrase" holds the phrase that is accessed in the methods. </p>
  
  <p>Here are the rules: </p>
  
  <p>The words are separated by spaces,
  It starts with a letter,
  It does not end with a space,
  There are never 2 consecutive spaces,
  There are never 2 consecutive digits or punctuation.</p>
  
  <p>Both the firstLetter() and lastLetter() methods must return an empty string if the phrase is empty. </p>
  
  <p>My question is: What is a more efficient solution to this problem? I am new to algorithms so I would appreciate a more seasoned approach to this simple problem. In the firstLetter() and the lastLetter() method, would I check the status of two characters at a time within the for loop or just one?</p>
  
  <p>Here is my code: </p>
  
  <pre><code>/**
   * Processes first and last letters of words
   * @author (Adrian DeRose)
   */
  public class StartToFinish
  {
      private String phrase;
  
      /**
       * Constructs a StartToFinish object
       * @param myString the phase for this object
       */
       public StartToFinish(String myString)
       {
           this.phrase = myString;
       }
  
      /**
       * Gets first letter of every word in string. 
       * 
       * @return first letter of every word in string
       */
      public String firstLetters()
      {   
          String firstLetters = "";
  
          if (Character.isLetter(this.phrase.charAt(0)))
          {
              firstLetters += this.phrase.substring(0,1);
          }
  
          for (int i = 1; i &lt; this.phrase.length(); i++)
          {
              char currentCharacter = this.phrase.charAt(i);
              String previousCharacter = Character.toString(this.phrase.charAt(i-1));
              if (Character.isLetter(currentCharacter) &amp;&amp; previousCharacter.equals(" "))
              {
                  String characterString = Character.toString(currentCharacter);
                  firstLetters += characterString;
              }
          }
          return firstLetters;
  
      }
  
      /**
       * Gets last letter of every word in string. 
       * 
       * @return last letter of every word in string
       */
      public String lastLetters()
      {
          String lastLetters = "";
          char lastCharacter = this.phrase.charAt(lastIndex);
  
          if (this.phrase.length() == 0)
          {
              return "";
          }
  
          for (int i = 1; i &lt; this.phrase.length(); i++)
          {
              char currentCharacter = this.phrase.charAt(i);
              char previousCharacter = this.phrase.charAt(i-1);
  
              if (Character.isLetter(previousCharacter) &amp;&amp; !Character.isLetter(currentCharacter))
              {
                  String previousCharacterString = Character.toString(previousCharacter);
                  lastLetters += previousCharacterString;
              }
          } 
  
          if (Character.isLetter(lastCharacter))
          {
              lastLetters += Character.toString(lastCharacter);
          }
  
          return lastLetters;
      }
  }
  </code></pre>
  
  <p>Thank you!</p>