Spring Semester 2005

Homework Six Example: Please note how the long javadoc comments replace your weekly reports.
import java.io.*;
 * In this program I solve the problem of counting the number of lines
 * in a file. This is problem one in homework six ( 
 * @author Adrian German 

public class One {

   /** The main method is only two lines long because a procedure (method) 
    * is used to count the lines in a file. The name of the file is passed 
    * on the command line and is available to the main method as the first 
    * of the command line arguments, args[0]. args[0] is passed to the 
    * One.countLines(String) method and the int returned by the method is
    * placed in the count variable (which is local to main). All of this is 
    * described on one line. The next line prints count and the program ends.

    public static void main(String[] args) {
	int count = One.countLines(args[0]); 
	System.out.println("There are " + count + " lines in file " + args[0]); 

    /** The countLines method receives one argument, called name, of type String.
     * It sets a local variable, count, to zero, It then tries to create a new
     * FileInputStream, called cable, using the name of the file on the disk. On
     * top of the cable we put receiver, which is an InputStreamReader, and on top
     * of that one we create the handset (a BufferedReader) which will allow us to
     * read lines from the file with the name "name". So we start by reading the
     * first line from the handset and then we enter a loop: while the line is not
     * the end of file, increment the count by one, and read a new line, repeat.
     * If the try fails an error message is printed. Otherwise the file input stream
     * is closed. The program returns count which is the number of lines read from 
     * the file. A return value of zero doesn't necessarily mean there was an error
     * since the file could have been empty. 

    public static int countLines(String name) {
        int count = 0; 
	try {
	    FileInputStream cable = new FileInputStream(name);
	    InputStreamReader receiver = new InputStreamReader(cable);
	    BufferedReader handset = new BufferedReader(receiver);
	    String line = handset.readLine();
	    while (line != null) {
                count = count + 1; 
		// System.out.println("(" + line + ")");
		line = handset.readLine();
	} catch (Exception e) {
	    System.out.println("Something went wrong: " + e.toString());
	return count;
Here's an example of my program in action:
frilled.cs.indiana.edu%ls -ld one.txt One.java
-rw-------  1 dgerman faculty 2461 Mar  5 17:41 One.java
-rw-------  1 dgerman faculty  319 Mar  5 17:41 one.txt
frilled.cs.indiana.edu%cat one.txt
this file                               
      is a test 
    my program 
it contains
    a                      quote
by soren kirkegaard
    goes like this: 
       Life   can only be 
   must be lived 
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One one
Something went wrong: java.io.FileNotFoundException: one (No such file or directory)
There are 0 lines in file one
frilled.cs.indiana.edu%java One one.txt
There are 16 lines in file one.txt

Last updated by Adrian German for A201/A597/I210 on Sat Mar 5 17:45:52 EST 2005