title: Sorting strings alphabetically in an array. Java
body: <p>I have to sort strings in an array for a school project. Our teacher won't allow us to use array,sort().
  i have to use 2 sort methods but they aren't working too well.</p>
  
  <p>The first one returns double of each value. ie John, jack, adam, tom will return adam,adam,jack,jack,john,john,tom,tom.</p>
  
  <pre><code>public static void sort() {
          inputFileNames();//inputs list of names from a file.
          for (int i = 0; i &lt; size - 1; i++) {
              for (int j = i + 1; j &lt; size; j++) {
                  if (stArr[i].compareTo(stArr[j])&gt;0) {
                      temp = stArr[i];
                      stArr[i] = stArr[j];
                      stArr[j] = temp;
                  }
              }
          }
          display("The names are: ");// method to display array 
          System.out.println("");
      }
  </code></pre>
  
  <p>the second sort doesn' run:</p>
  
  <pre><code>public static void bubbleSort() {
          inputFileNames();
          for (int i = size - 1; i &gt;= 0; i--) {
              for (int j = 0; j &lt;= i; j++) {
                  if (stArr[j].compareTo(stArr[j+1])&gt;0) {
                      temp = stArr[j];
                      stArr[j] = stArr[j + 1];
                      stArr[j + 1] = temp;
                  }
              }
          }
          display("The names are: ");
          System.out.println("");
      }
  </code></pre>
  
  <p>input and display:</p>
  
  <pre><code>static void display(String heading) {
      System.out.println(heading + "\n");
      for (int i = 0; i &lt; size; i++) {
          System.out.println(stArr[i]);
      }
  }
  
  static void inputFileNames() {
      try {
          Scanner scFile = new Scanner(new File("Names.txt"));
          while (scFile.hasNext()) {
              stArr[size] = scFile.nextLine();
              size++;
          }
      } catch (FileNotFoundException ex) {
          System.out.println("File not found.");
      }
  }
  </code></pre>