THINK FIRST·CODE LATER

← All labs

Counting the keywords of a Java program

Problem

Write KeywordCounter, which reads Java source code from the input and counts its keywords (class, int, if, while, …; include true, false and null).

  • Put the keywords in a HashSet: testing whether a word is a keyword is then one contains call.
  • Words inside // comments, /* … */ comments, string literals and character literals must not be counted: first replace those parts by a space.
  • Count each keyword in a TreeMap, so that the report is alphabetical, and collect the other identifiers in a set to know how many different ones there are.

For the input

public class Demo {
    // this comment mentions while and for, which must not be counted
    private static final int LIMIT = 10;

    public static void main(String[] args) {
        int total = 0;
        for (int i = 0; i < LIMIT; i++) {
            if (i % 2 == 0) {
                total += i;          /* else, return and class inside a comment */
            } else {
                continue;
            }
        }
        String text = "if this were code, new and try would count";
        char quote = '"';
        while (total > 0) {
            total = total / 2;
        }
        System.out.println(text + total + quote);
    }
}

the program prints

Keywords: 17 (13 different)
  char: 1
  class: 1
  continue: 1
  else: 1
  final: 1
  for: 1
  if: 1
  int: 3
  private: 1
  public: 2
  static: 2
  void: 1
  while: 1
Different identifiers: 12

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d5-7-data-1.txt — first 14 lines of 21

public class Demo {
    // this comment mentions while and for, which must not be counted
    private static final int LIMIT = 10;

    public static void main(String[] args) {
        int total = 0;
        for (int i = 0; i < LIMIT; i++) {
            if (i % 2 == 0) {
                total += i;          /* else, return and class inside a comment */
            } else {
                continue;
            }
        }
        String text = "if this were code, new and try would count";
What your program must print for d5-7-data-1.txt
Keywords: 17 (13 different)
  char: 1
  class: 1
  continue: 1
  else: 1
  final: 1
  for: 1
  if: 1
  int: 3
  private: 1
  public: 2
  static: 2
  void: 1
  while: 1
Different identifiers: 12

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d5-7-data-1.txt

Windows PowerShell does not understand <:

Get-Content d5-7-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.