title: java.lang.StringIndexOutOfBoundsException: String index out of range: 7
body: <p>I don't really know how to program... I was working on this for a Computer Science Class</p>
  
  <blockquote>
    <p>Instruction: Use nested loops to print out the square word pattern show below.
    I'm guessing the error is in the toString method, but I can't spot where.</p>
  </blockquote>
  
  <p>the desired output is: (when input is SQUARE)</p>
  
  <pre><code>SQUARE
  Q    R
  U    A
  A    U
  R    Q
  ERAUQS
  </code></pre>
  
  <p>The code:
      import static java.lang.System.*;</p>
  
  <pre><code>class BoxWord
  {
     private String word;
  
   public BoxWord()
   {
    word="";
   }
  
   public BoxWord(String s)
   {
     setWord(s);
   }
  
   public void setWord(String w)
   {
     word=w;
   }
  
   public String toString()
   {
    String output=word +"\n";
    for(int i =0;i&lt;word.length(); i++){
      output += word.charAt(i);
      for(int j = 2; j&lt;word.length();j++)
        output += " ";
      output+= word.charAt(word.length()-(i-1))+ "\n";
    }
  
    for(int k=0; k&lt;word.length(); k++)
     output+= word.charAt(k);
  
  
    return output+"\n";
   }
  }
  </code></pre>
  
  <p>main:</p>
  
  <pre><code>import static java.lang.System.*;
  
  public class Lab11f
  {
     public static void main( String args[] )
     {
       BoxWord test = new BoxWord("square");
       out.println(test);   
  
   }
  }
  </code></pre>