title: Java - Finding Largest and Smallest Numbers using an Array
body: <p>I am supposed to make a program that takes 10 numbers from a user input, find the largest and smallest number and display all the inputs from the user. This program does use an array. Here is my code:</p>
  
  <pre><code>import java.util.Scanner; // program uses Scanner
  public class ArrayTester {
      // begin execution
      public static void main(String[] args) {
          // declare and create array object
          // declare smallest and largest int variables 
          int[] numbers;
          numbers = new int[10];
          int smallest = numbers[0], largest = numbers[0];
  
          // create Scanner object
          Scanner input = new Scanner(System.in);
  
          // prompt user 
          System.out.print("Please enter 10 numbers: \n");
          // use for loop to obtain user input
          for (int counter = 0; counter &lt; numbers.length; counter++) {
              numbers[counter] = input.nextInt();
          } // end obtaining input
  
          // enhanced for loop to find largest and smallest values
          for (int i : numbers) {
              if (numbers[i] &lt; smallest) {
                  smallest = numbers[i];
              } // end finding smallest
              else if (numbers[i] &gt; largest) {
                  largest = numbers[i];
              } // end finding largest number 
          } // end finding largest and smallest values
  
          // for loop to print user input 
          System.out.printf("%s%8s\n", "Index", "Input");
          for (int counter = 0; counter &lt;= numbers.length; counter++) {
              System.out.printf("%5d%8d\n", counter, numbers[counter]);
          } // end printing input values
  
          // print smallest and largest numbers
          System.out.printf("Smallest number: %d\nLargest number: %d\n", smallest, largest);
          System.out.print("Programmed by Christian Lapinig");
      } // end main
   } // end ArrayTester
  </code></pre>
  
  <p>At this point I am able to obtain user inputs, but I run into this problem:</p>
  
  <pre><code>Please enter 10 numbers: 
  454
  392
  33
  41
  6
  44
  39
  21
  12
  2
  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 454
  at ArrayTester.main(ArrayTester.java:32)
  </code></pre>
  
  <p>Would I need a try and catch block to fix this?</p>