title: Sorting names entered by the user in alphabetical order according to the last name
body: <p>I have completed most of the code by myself (with the help of a bit of Googling) but I have run into an unexpected problem. First-off, I have to sort a user entered list of names in aplhabetical order of their last names using selection sort. Here is my code:</p>
  
  <pre><code>    import java.util.*;
  class Name_Sort
  {
      public static void main (String args[])
      {
          Scanner in = new Scanner (System.in);
          System.out.print ("Enter the number of names you wish to enter: ");
          int n = in.nextInt();
          String ar[] = new String [n];
          for (int i = 0; i&lt;ar.length; i++)
          {
              System.out.print("Please enter the name: ");
              ar[i]= in.nextLine();
          }
          String temp;
          for (int b = 0; b&lt;n; b++)
          {
              for (int j=b+1; j&lt;n; j++)
              {
                  if ((compareLastNames(ar[b], ar[j]))&gt;0)
                  {
                      temp = ar[b];
                      ar[b] = ar[j];
                      ar[j] = temp;
                  }
              }
          }
          System.out.println ("The names sorted in alphabetical order are: ");
          for (int a = 0; a&lt;n; a++)
              System.out.print (ar[a]+"\t");
      }
  
      private static int compareLastNames(String a, String b) 
      {
          int index_a = a.lastIndexOf(" ");
          String surname_a = a.substring(index_a);
          int index_b = b.lastIndexOf(" ");
          String surname_b = b.substring(index_b);
          int lastNameCmp = surname_a.compareToIgnoreCase(surname_b);
          return lastNameCmp;
      }
  }
  </code></pre>
  
  <p>The problem (I think) is arising when I'm taking the names from the user, specifically, this part:</p>
  
  <pre><code>Scanner in = new Scanner (System.in);
      System.out.print ("Enter the number of names you wish to enter: ");
      int n = in.nextInt();
      String ar[] = new String [n]; //Array to store the names in.
      for (int i = 0; i&lt;ar.length; i++)
      {
          System.out.println("Please enter the name: ");
          ar[i]= in.nextLine();
  
      }
  </code></pre>
  
  <p>The output on the terminal window of BlueJ shows up as</p>
  
  <pre><code>Name_Sort.main({ });
  Enter the number of names you wish to enter: 5
  Please enter the name: 
  Please enter the name: 
  </code></pre>
  
  <p>That is not what it's supposed to display. What could I be doing wrong? I've pondered over it for a while, but nothing comes to mind.</p>
  
  <p>And, even if I do move forward and enter a few names despite the error above, I get another error in this part of my code here:</p>
  
  <pre><code>private static int compareLastNames(String a, String b) 
  {
      int index_a = a.lastIndexOf(" ");
      String surname_a = a.substring(index_a);// This is the line the compiler highlights.
      int index_b = b.lastIndexOf(" ");
      String surname_b = b.substring(index_b);
      int lastNameCmp = surname_a.compareToIgnoreCase(surname_b);
      return lastNameCmp;
  }
  </code></pre>
  
  <p>the error is :</p>
  
  <pre><code>java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (injava.lang.String)
  </code></pre>
  
  <p>Does this mean that the white-space character <code>" "</code> is not present? But why?</p>
  
  <p>This is a screenshot of the terminal window:
  <a href="http://imgur.com/l7yf7Xn" rel="nofollow">http://imgur.com/l7yf7Xn</a></p>
  
  <p>The thing is, if I just initialize the array with the names first (and not take any input from the user) the codes runs fine and produces the desired result. Any help please?</p>
  
  <p>Also, since I know some people here are very particular about this, yes, this is a homework assignment, yes, I did do all of the code by myself, I googled on how to sort the names in alphabetical order as I couldn't exactly code out the original idea I had. 
  Which was comparing the ASCII values of each character of two surnames to see which should come first. Like: <code>if((int) surname1.charAt(0)&gt;(int) surname2.charAt(0))</code> then surname2 should come before surname1, else if they both have the same first character, take the second character and so on.</p>
  
  <p>Thanks for taking the time to read this.</p>