public class ReadAllNumbers
1: import java.util.Scanner;
2: import java.util.ArrayList;
3: import java.util.Collections;
4: import java.io.File;
5: import java.io.FileNotFoundException;
7: public class ReadAllNumbers {
9: public static void main(String[] args)
10: throws FileNotFoundException {
12: // create required variables
13: Scanner kbd = new Scanner(System.in);
14: ArrayList<Double> nums = new ArrayList<Double>();
15: Scanner fin;
16: String name;
17: double num;
19: // read all the numbers from a file named by the user
20: System.out.print("Enter the name of the file: ");
21: name = kbd.nextLine();
22: fin = new Scanner(new File(name));
23: while (fin.hasNextDouble()) {
24: nums.add(fin.nextDouble());
25: }
26: fin.close();
28: // Report some of what you found
29: System.out.println("\n" + name + " has " + nums.size()
30: + " numbers in it.\n"
31: + "The first is " + nums.get(0)
32: + " and the last is " + nums.get(nums.size() - 1) + ".\n");
34: // Sort and print the list, 5 #s per line
35: Collections.sort(nums);
36: System.out.println("Sorted, those numbers were: ");
37: for (int i = 0; i < nums.size(); ++i) {
38: if (i % 5 == 0) {
39: System.out.println();
40: }
41: System.out.printf("%15.2f", nums.get(i));
42: }
43: System.out.println("\n\n");
44: }
45: }