title: How to get rid of the zeros at the end of sorted integers
body: <p>My code is working but it gives me unnecessary number and I don't know how to get rid of them.Please any suggestion would be highly appreciated</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.*;
  
  
   public class ArraySort1 {
  
    public static void main(String[] args) throws IOException {
    Scanner inputFile; 
    inputFile = new Scanner(new File("arrayex1.txt"));
    PrintWriter outputFile;
  outputFile = new PrintWriter(new FileWriter("sorted.txt"));
  
        /* Declare the array */
  
  int[] numbers = new int[10000];
  int index = 0;
  
        int temp, smallest, smallest_index;
  
        /* Read the first number */
        numbers[index] = inputFile.nextInt();
  
        while(inputFile.hasNextInt()) {
          numbers[index] = inputFile.nextInt();
          index++;
          }
  // Local variable
  int swValue;
  
  // Display menu graphics
  System.out.println("============================");
  System.out.println("|   MENU SELECTION DEMO    |");
  System.out.println("============================");
  System.out.println("| Options:                 |");
  System.out.println("|        1. Selection Sort |");
  System.out.println("|        2.  Bubble Sort   |");
  System.out.println("|        3. Exit           |");
  System.out.println("============================");
  swValue = Keyin.inInt(" Select option: ");
  
  // Switch construct
  switch (swValue) {
  case 1:
    System.out.println(" Selection Sort Starting....");
    for (int i = 0; i &lt; index - 1; i++) {
  
        smallest = numbers[i];
        smallest_index = i;
  
        for (int j = i + 1; j &lt; index; j++) {
  
          if (numbers[j] &lt; smallest) { 
                smallest = numbers[j];
                smallest_index = j;
            }
        }
  
        /* If needed switch numbers[i] and numbers[smallest_index] */
  
        if (numbers[i] != numbers[smallest_index]) {
  
            temp = numbers[i];
            numbers[i] = numbers[smallest_index];
            numbers[smallest_index] = temp;
        }
    }
    for (int i = 0; i &lt; index; i++) {          
      System.out.print (numbers[i] + " ");
            outputFile.print (numbers[i] + " ");
        }
  
        inputFile.close();
        outputFile.close();
  
  
  
    break;
  case 2:
   //2 System.out.println("Bubble Sort Sorting 2 selected");
  
      //int i = 0, t=0;
      boolean swapped = true;
  
      while(swapped){
  
      swapped = false;
  
      for(int i1 = 0; i1 &lt; index  - 1; i1++){
          smallest = numbers[i1];
          smallest_index = i1;
  
      if(numbers[i1] &gt; numbers[i1 + 1]){
      swapped = true;
      int tmp = numbers[i1];
      numbers[i1] = numbers[i1 + 1];
      numbers[i1 + 1] = tmp;
      } 
      } 
  
      for(int i1 = 0; i1 &lt; numbers.length; i1++){
     System.out.print(numbers[i1] + "\t");
      } 
     System.out.println();
  
      }
  
  
  
  
   break;
  
  case 3:
    System.out.println("Exit selected");
    break;
  default:
    System.out.println("Invalid selection");
    break; // This break is not really necessary
     }
   }
  
  }
  </code></pre>
  
  <p>If I Select option 2 : Bubble sort I get</p>
  
  <pre><code>1   2   4   1   5   7   9   56  67  0   0   0
  1   2   4   5   7   9   56  67   0               0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   
  1   1   2   4   5   7   9   56  67  0
  </code></pre>
  
  <p>Please the code where I have problem is the Bubble sort.
  Thanks in advance.</p>