Source of ArraySorter.java


  2: import java.util.Scanner;
  3: import java.util.Arrays;

  5: /**
  6:  * A program to demonstrate the sort method in java.util.Arrays.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class ArraySorter {

 12:     // the size of the array
 13:     public static final int MAX = 5;

 15:     public static void main(String[] args) {
 16:         // create variables
 17:         Scanner kbd = new Scanner(System.in);
 18:         int[] a = new int[MAX];
 19:         double[] b = new double[MAX];
 20:         String[] c = new String[MAX];

 22:         // read integers
 23:         System.out.println("\n\nEnter " + MAX + " integer values below:");
 24:         for (int i = 0; i < MAX; ++i) {
 25:             a[i] = kbd.nextInt();
 26:         }
 27:         kbd.nextLine();

 29:         // report and sort integers
 30:         System.out.println("\nThe numbers you entered were: "
 31:                 + Arrays.toString(a));
 32:         Arrays.sort(a);
 33:         System.out.println("Sorted, those numbers are: "
 34:                 + Arrays.toString(a));

 36:         // read doubles
 37:         System.out.println("\n\nEnter " + MAX + " double values below:");
 38:         for (int i = 0; i < MAX; ++i) {
 39:             b[i] = kbd.nextDouble();
 40:         }
 41:         kbd.nextLine();

 43:         // report and sort doubles
 44:         System.out.println("\nThe numbers you entered were: "
 45:                 + Arrays.toString(b));
 46:         Arrays.sort(b);
 47:         System.out.println("Sorted, those numbers are: "
 48:                 + Arrays.toString(b));

 50:         // read words
 51:         System.out.println("\n\nEnter " + MAX + " words below:");
 52:         for (int i = 0; i < MAX; ++i) {
 53:             c[i] = kbd.next();
 54:         }
 55:         kbd.nextLine();

 57:         // report and sort words
 58:         System.out.println("\nThe words you entered were: "
 59:                 + Arrays.toString(c));
 60:         Arrays.sort(c);
 61:         System.out.println("Sorted, those words are: "
 62:                 + Arrays.toString(c));

 64:         // sort words ALPHABETICALLY!
 65:         Arrays.sort(c, String.CASE_INSENSITIVE_ORDER);
 66:         System.out.println("Sorted /alphabetically/, those words are: "
 67:                 + Arrays.toString(c));

 69:         // nice extra space at end
 70:         System.out.println("\n\n");
 71:     }

 73: }