/** * CommandLine.java * Jeff Ondich, Carleton College, 2014-01-05 * * A copy of LineReader.java that takes the input file path from the command * line rather than from a hard-coded string. * * This is the Java half of a pair of parallel examples in Python and Java. * See commandline.py. */ import java.io.File; import java.util.Scanner; import java.io.FileNotFoundException; public class CommandLine { public static void main(String[] args) { // START CHANGES // This is the only part that's different from LineReader.java if (args.length == 0) { System.err.println("Usage: java CommandLine inputFilePath"); System.exit(1); } String inputFilePath = args[0]; // END CHANGES File inputFile = new File(inputFilePath); Scanner scanner = null; try { scanner = new Scanner(inputFile); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); } int numberOfLines = 0; while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line.toUpperCase()); numberOfLines++; } System.out.println("\nNumber of lines: " + numberOfLines); } }