title: Adding Spaces between words and making every word except the first lowercase in java
body: <p>I'll go ahead and let you know that yes, this is homework. I have hit a brick wall in completing it however and desperately need help. I'm also pretty new to Java and am still learning the language.</p>
  
  <p>Okay, I am trying to write a program that asks the user to enter a sentence with no spaces but have them capitalize the first letter of each word. The program should then add spaces between the words and have only the first word capitalized, the rest should start with a lowercase. I can get the space inserted between the words, but I cannot get the first letter of each word lower-cased. I have tried several different ways, and the latest one is giving me this error message:</p>
  
  <pre><code> Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
   ex out of range: 72
      at java.lang.AbstractStringBuilder.setCharAt(Unknown Source)
      at java.lang.StringBuilder.setCharAt(Unknown Source)
      at renfroKristinCh9PC14.main(renfroKristinCh9PC14.java:45)
  </code></pre>
  
  <p>I'm posting up my code and any and all help you can give me will be very much appreciated.
  Thanks.</p>
  
  <pre><code>/*
  This program will ask the user to enter a sentence without whitespaces, but 
  with the first letter of each word capitilized. It will then separate the words
  and have only the first word of the sentence capitalized.
  */
  
  import java.util.*;
  
  public class renfroKristinCh9PC14
  {
  public static void main(String[] args)
  {
      //a string variable to hold the user's input and a variable to hold the modified sentence
      String input = "";
      //variable to hold a character
      char index;
  
      //create an instance of the scanner class for input
      Scanner keyboard = new Scanner(System.in);
  
      //welcome the user and explain the program
      userWelcome();
  
      //get the sentence from the user
      System.out.println("\n  Please enter a sentence without spaces but with the\n");
      System.out.println("  first letter of each word capitalized.\n");
      System.out.print("  Example: BatmanIsTheBestSuperheroEver!  ");
  
      input = keyboard.nextLine();
  
      //create an instance of the StringBuilder class
      StringBuilder sentence = new StringBuilder(input);
  
      //add spaces between the words
      for(int i=0; i &lt; sentence.length(); i++)
      {
          index = sentence.charAt(i);
          if(i != 0 &amp;&amp; Character.isUpperCase(index))
          {
  
              sentence.setCharAt(index, Character.toLowerCase(index));
              sentence.append(' ');
  
  
          }
  
          sentence.append(index);
  
      }   
  
  
      //show the new sentence to the user
      System.out.println("\n\n  Your sentence is now: "+sentence);
  }
  /***********************************************************************************    *************************
  ************************************************************************************    *************************
  
  This function welcomes the user and exlains the program
  */
  
  public static void userWelcome()
  {
  
      System.out.println("\n\n       ****************   ****************************************************\n");
      System.out.println("  *            Welcome to the Word Seperator Program                   *");
      System.out.println("  *  This application will ask you to enter a sentence without       *");
      System.out.println("  *  spaces but with each word capitalized, and will then alter the  *");
      System.out.println("  *  sentence so that there arespaces between each word and          *");
      System.out.println("  *  only the first word of the sentence is capitalized              *");
      System.out.println("\n  ********************************************************************\n");
  }
  </code></pre>
  
  <p>}</p>