title: Why does this array go out of bounds inconsistently?
body: <p>This is a Student class I have which creates a student that has a name, grade, and ID number.  The Student is used as the key in a TreeMap, while the student's grade is used as the value.  I wrote compareTo as I am implementing Comparable, but this error pops up upon entering in the first student:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
      at Student.&lt;init&gt;(Student.java:25)
      at Main.main(Main.java:50)
  Java Result: 1
  </code></pre>
  
  <p>I think it is coming from the splitting of the student's name into a String[], but I've tested those lines that call the first and last names, as well as the line that splits the name and they all seem to work as intended, except for when they are called in compareTo.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <pre><code>public class Student implements Comparable&lt;Student&gt;{
      private String name, grade, first, last;
      private String[] stuName;
      private int id;
  
      /**
       * Constructs a new Student with a name, ID Number, and a grade.
       * @param n name
       * @param i ID Number
       * @param g grade
       */
      public Student(String n, int i, String g){
          name = n;
          id = i;
          grade = g;
          stuName = n.split(" ");
          first = stuName[0];
          last = stuName[1];
      }
  
      /**
       * Compares Students. First by last name, then by first if the last names
       * are the same.  If the first names are also the same, the students are
       * compared by their ID Numbers.
       * @return the value upon comparing the proper property of the Student
       */
      @Override
      public int compareTo(Student other){
          if (last.equals(other.getLast())){
              if (first.equals(other.getFirst())){
                  return id - other.getID();
              }else
                  return first.compareTo(other.getFirst());
          }
          return last.compareTo(other.getLast());
      }
  
      /**
       * Changes the student's current grade.
       * @param g new grade
       */
      public void changeGrade(String g){
          grade = g;
      }
  
      /**
       * Returns student's name.
       * @return name
       */
      public String getName(){
          return name;
      }
  
      /**
       * Returns student's first name.
       * @return first name
       */
      public String getFirst(){
          return first;
      }
  
      /**
       * Returns student's last name.
       * @return last name
       */
      public String getLast(){
          return last;
      }
  
      /**
       * Returns the student's grade.
       * @return grade
       */
      public String getGrade(){
          return grade;
      }
  
      /**
       * Returns the student's ID Number
       * @return id number
       */
      public int getID(){
          return id;
      }
  </code></pre>
  
  <p>Tester:</p>
  
  <pre><code>public class Main {
      public static void main(String[] args) throws InterruptedException{
          Map&lt;Student, String&gt; students = new TreeMap();
          Scanner in = new Scanner(System.in);
          System.out.println("How many students do you want to add?");
          int numStudents = in.nextInt();
          String name, grade;
          int id;
  
  
          for (int i = 1; i &lt; numStudents; i++){
              System.out.println("Name of student " + i + "?");
              name = in.nextLine();
              in.nextLine();
  
              System.out.println("Grade of " + i + "?");
              grade = in.nextLine();
  
              System.out.println("ID Number of " + i + "?");
              id = in.nextInt();
  
              Student s = new Student(name, id, grade);
              students.put(s, s.getGrade());
          }
  
  
          System.out.println("How many students do want to remove");
          int remStudents = in.nextInt();
  
          for (int i = 0; i &lt; remStudents; i++){
              System.out.println("ID?");
              int remID = in.nextInt();
  
              for (Student s : students.keySet()){
                  if (s.getID() == remID){
                      students.remove(s);
                  }
              }
          }
  
  
          System.out.println("How many grades do you want to change?");
          int changeGrades = in.nextInt();
  
          for (int i = 0; i &lt; changeGrades; i++){
              System.out.println("ID?");
              int foo = in.nextInt();
              System.out.println("New grade?");
              String newGrade = in.nextLine();
  
              for (Student s : students.keySet()){
                  if (s.getID() == foo){
                      s.changeGrade(newGrade);
                  }
              }
          }
  
          String printout = "";
          for (Student s : students.keySet()){
              printout += s.getLast() + ", " + s.getFirst() + " (" + s.getID() + "): " + s.getGrade() + "\n";
          }
          System.out.println(printout);
      }
  }
  </code></pre>