// LineCount.java // Count how many lines are in a file of text // P. Conrad, for UCSB CS5JA, 03/05/2008 public class LineCount { public static void main(String args[]) { // Make sure there is exactly one argument if (args.length!=1) { System.out.println("Error: you should pass exactly one argument"); System.out.println("That argument should be the filename"); System.exit(1); // non zero status code signifies an error } // Store the first argument in the variable "filename" String filename = args[0]; // Try to open the file try { Scanner fileInput; fileInput = new Scanner(new BufferedReader(new FileReader(filename))); } catch (IOException e) { System.out.println("Error: could not open " + filename); System.out.println("The error was " + e.toString()); System.exit(2); // error number two! } System.out.println("Successfully opened the file " + filename); System.out.println("Now, we'll count the lines..."); // If that worked, read through every line in the file // and count how many there are // @@@ // Output the result // @@@ } }