WidthChecker.java


Below is the syntax highlighted version of WidthChecker.java from §3.1 Using Data Types.


/*************************************************************************
 *  Compilation:  javac WidthChecker.java
 *  Execution:    java WidthChecker N < source.java
 *  Dependencies: In.java
 *  
 *  Reads in a text file and prints out all lines that have (strictly)
 *  more than N characters (including spaces but not the newline 
 *  character itself).
 *
 *  Note: setting parameter N = -1 prints out all lines with line numbers.
 *
 *************************************************************************/

public class WidthChecker { 
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);       // max line length

        In stdin = new In();
        for (int cnt = 1; true; cnt++) {
            String line = stdin.readLine();      // read in next line
            if (line == null) break;             // no more input
            if (line.length() > N)               // line exceeds threshold
                System.out.println(cnt + ": " + line);
        }
    }

}


Last updated: Wed Oct 20 07:15:41 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.