title: IndexOutOfBoundsException error while trying to print first 4 elements of an ArrayList
body: <p>...output:<br>
      Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out-of-bounds for length 1
          at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)
          at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source)
          at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source)
              at java.base/java.util.Objects.checkIndex(Unknown Source)
              at java.base/java.util.ArrayList.get(Unknown Source)
              at HelloWorld.main(HelloWorld.java:27) ...output</p>
  
  <blockquote>
    <blockquote>
      <p>My program is a to-do list and is as follows:</p>
    </blockquote>
  </blockquote>
  
  <p>...Java............................................<br>
      import java.util.Scanner; 
      import java.util.ArrayList;</p>
  
  <pre><code>/**
   * @author Troy
   *
   */
  public class HelloWorld {
  
      /** A very simple to-do list that asks for input and then prints out the  list 
       * in the form of an ArrayList: eg:
       Here is today's to-do list:
       [Wake up, Walk the dog,Eat breakfast, Make the bed]
        */
      public static void main(String[] args) {
          // I chose an ArrayList because the size does not have to be predetermined.
          ArrayList&lt;String&gt; to_do = new&lt;String&gt;ArrayList();
          System.out.println("What would you like to add to your to-do list?");
          Scanner user_input = new Scanner(System.in);
          //While the user_input still has entries, perform the following:
          while (user_input.hasNextLine()) {
              //Add next entry in the to-do list(user_input) to the ArrayList
              to_do.add(user_input.nextLine());
              System.out.println("\n");
              /**Print out the ArrayList(to_do) 4 elements at a time. */
              for (int i = 0; i &lt; 5; i++ ) {
                  System.out.println("Your list for today is: " + "\n" + to_do.get(i));
  
              }
  
              }
      }
  }
  </code></pre>
  
  <p>...Java......................................................................</p>
  
  <blockquote>
    <p>I get the above error message as soon as I write the to_do.get(i) at the very end of the program.</p>
    
    <p>Another bonus question is how to end the while loop without affecting the output of the final ArrayList?</p>
  </blockquote>