/* Program: SortEx * Author: Dr. Blaise W. Liffick * Date: November 2007 * * Description: This program reads in exactly 20 values from a file into * an array, prints that array, then sorts the array and prints it again. */ import java.io.*; import java.util.*; public class SortEx { public static void main(String[] args) throws FileNotFoundException { int count = 0; Scanner input = new Scanner(new File("initial.txt")); int[] values = new int[20]; // read in the data from the file while (input.hasNextInt()) { int next = input.nextInt(); values[count] = next; count++; } printVals(values); System.out.println(); Arrays.sort(values); //sort the array, which changes the array contents System.out.print("Sorted "); printVals(values); } //----------------------------------------- // Print out the contents of an array, one per line public static void printVals(int [] vals) { System.out.println("Values:"); for (int i = 0; i < vals.length; i++) { System.out.println(i + ":\t" + vals[i]); } } }