title: java.lang.ArrayIndexOutOfBoundsException: 3 in a while loop
body: <p>I've been getting this exception and I've no idea how to go about fixing it:</p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: 3
  </code></pre>
  
  <p>in the <code>while</code> loop. Here's my code:</p>
  
  <pre><code>public class NameSearch {
      static String[] names = new String[3];
  
      void populateStringArray() {
          names[0] = "Ben";
          names[1] = "Thor";
          names[2] = "Zoe";
          names[3] = "Kate";
      }
  
      public static void main(String[] args) {
          String pName;
          int max = 4;
          int current = 1;
          boolean found = false;
  
          Scanner scan = new Scanner(System.in);
          System.out.println("What player are you looking for?");
          pName = scan.next();
          while (found == false &amp;&amp; current &lt;= max) {
              if (names[current] == pName) {
                  found = true;
              } else {
                  current = current + 1;
              }
          }
          if (found == true) {
              System.out.println("Yes, they have a top score");
          } else {
              System.out.println("No, they do not have a top score");
          }
      }
  }
  </code></pre>
  
  <p>The code is meant to ask the user to input a name and it will check to see if the name is in the array (in a nutshell).</p>
  
  <p>My IDE (Eclipse) says that the error lies in the line <code>if (names[current] == pName){</code>.</p>