title: Find all substrings of a string - StringIndexOutOfBoundsException
body: <p>I created class Word. Word has a constructor that takes a string argument and one method getSubstrings which returns a String containing all substring of word, sorted by length.</p>
  
  <p>For example, if the user provides the input "rum", the method returns a
  string that will print like this:</p>
  
  <pre><code>r
  u
  m
  ru
  um
  rum 
  </code></pre>
  
  <p>I want to concatenate the substrings in a String, separating them with a newline ("\n"). Then return the string.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>    public class Word {
      String word;
  
      public Word(String word) {
          this.word = word;
      }
      /**
       * Gets all the substrings of this Word.
       * @return all substrings of this Word separated by newline
       */
  
      public String getSubstrings()
      {
          String str = "";
          int i, j;
          for (i = 0; i &lt; word.length(); i++) {
              for (j = 0; j &lt; word.length(); j++) {
                  str = word.substring(i, i + j);
                  str += "\n";
              }
          }
          return str;
      }
  </code></pre>
  
  <p>But it throws exception:</p>
  
  <pre><code>java.lang.StringIndexOutOfBoundsException: String index out of range: -1
      at java.lang.String.substring(String.java:1911)
  </code></pre>
  
  <p>I stuck at this point. Maybe, you have other suggestions according this method signature <code>public String getSubstrings()</code>.<br>
  How to solve this issue?</p>