title: Java: Selection sort of names entered by the user in alphabetical order
body: <p>I already have a code for Selection sort using an array:</p>
  
  <pre><code>public class SelectionSortTest
  { 
  
   public static void main(String[] args)
   {
        String[] stringArray = {"Flash", "Green Arrow", "superman", "spiderman", "green lantern"};
  
        System.out.println("Unsorted array:");
        for(String element: stringArray)
             System.out.print(element + " ");
        System.out.println();
  
        selectionSort(stringArray);
  
        System.out.println("Sorted array:");
        for(String element: stringArray)
              System.out.print(element + " ");
        System.out.println();
   }
  
  
   static void selectionSort(Comparable[] array)
   {
        int smallindex;
        for(int i = 0; i &lt; array.length; i++)
        {
             smallindex = i; // set first element as smallest
             for(int j = i + 1; j &lt; array.length; j++) // find smallest
                  if(array[j].compareTo(array[smallindex]) &lt; 0)
                       smallindex = j;
             if(smallindex != i)
                  swap(array, smallindex, i);
        }
   }
  
  
   static void swap(Object[] array, int index1, int index2)
   {
        Object temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
   } 
  }
  </code></pre>
  
  <p>But i want to change this so that the user has to input those names instead. Such that the outprint keeps asks "Enter the names" and you put whatever amount of names you want (separated by commas) then does the above task.</p>
  
  <p>i Have tried replacing </p>
  
  <pre><code>String[] stringArray = {"Flash", "Green Arrow", "superman", "spiderman", "green lantern"}; 
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>Scanner input = new Scanner(System.in);
  
  for (int i = 0; &lt; array.length; i++)
  {
  System.out.print("Enter names " + (i + 1) + ": ");
  array[i] = input.next():    
  </code></pre>
  
  <p>But it doesn't work.  Any suggestions?  </p>