Programming 2 with Java

Exercise: Line Counter


  1. Write a program that counts the lines of Java files in a directory tree, broken down into empty lines, comment lines, annotations and code lines.
  2. Use the walk method of the class Files class to traverse the directory tree, and the lines method to read the lines of a file.
  3. Define an enumeration LineType for the different kinds of lines and use the following helper function to classify them:
    private static LineType classify(String line) {
    	line = line.trim();
    	if (line.isEmpty()) return LineType.EMPTY;
    	if (line.startsWith("//")) return LineType.COMMENT;
    	if (line.startsWith("@")) return LineType.ANNOTATION;
    	return LineType.CODE;
    }
    

Solution