title: Not able to find the indexOf the firstWord after trimming the text
body: <p>Hey there fellow programmers,</p>
  <p>I'm having a little trouble with this program of mine, it's supposed to read in a line of text, then output a line of text so that the first word and the last word are swapped. The only problem is when I try to find the first position in the line after using the trim() method, it sets the index as 0 and doesn't display the firstWord. If someone could gracefully fix &amp; explain my mistake, I would gladly appreciate it. By the way, I am new to programming.</p>
  <pre><code>public static void main(String[] args) {
      
      //Text
      String txt = &quot;  one two three four five&quot;;
      
      //use trim method to remove spaces before the firstWord and after lastWord
      String firstWord = txt.trim();
      String lastWord = txt.trim();
  
      //use indexOf to find the first space position in the line
      int index = txt.indexOf(' ');
      
      //use substring to get firstWord and the rest of the line
      firstWord = txt.substring(0, index);
      
      //use lastIndexOf to find the last space position in the line
      int lastIndex = txt.lastIndexOf(' ');
      
      //use substring to find the last word
      lastWord = txt.substring(lastIndex);
      
      //form a new string using concatenation
      System.out.println(lastWord + &quot; &quot; + firstWord);
      
  }
  </code></pre>