Free Response Practice Question (with solution)- Count Lines and Words in Text Files


<-- Back to AP CSA Exam Solutions Next to Characters and Alphabets Count -->




Count Lines and Words in Text Files

Write a Java program that reads text from a file, named content.txt and:

  1. counts and prints the number of lines in it.
  2. counts and prints the number of words in it.
  3. counts and prints the number of words in each line.
  4. prints only those lines which have even number of words.

Sample Input: content.txt


Sample Output (a): counts and prints the number of lines in it.

View Output


Solution for (a): counts and prints the number of lines in it.

View Solution

CountLinesRunner.java


import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class CountLinesRunner {

	public static void main(String args[]) {
		try {
			File inputFile = new File("content.txt");
			Scanner input = new Scanner(inputFile);
			int count = 0;
			while (input.hasNext()) {
				input.nextLine();
				count++;
			}
			System.out.println("The total number of lines: " + count);
			input.close();
		} catch (IOException e) {
			System.out.println("File not found: " + e.getMessage());
		}

	}
}



Sample Output (b): counts and prints the number of words in it.

View Output

Solution for (b): counts and prints the number of words in it.

View Solution

CountWordsRunner.java


import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class CountWordsRunner {

	public static void main(String args[]) {
		try {
			File inputFile = new File("content.txt");
			Scanner input = new Scanner(inputFile);
			int count = 0;
			String[] words = null;
			while (input.hasNext()) {
				String line = input.nextLine();
				words = line.split(" ");
				count = count + words.length;
			}

			System.out.println("Total Word count: " + count);
			input.close();

		} catch (IOException e) {
			System.out.println("File not found: " + e.getMessage());
		}

	}
}




Sample Output (c): counts and prints the number of words in each line.

View Output

Solution for (c): counts and prints the number of words in each line.

View Solution

WordCountTextRunner.java


import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class WordCountTextRunner {

	public static void main(String args[]) {
		try {
			File inputFile = new File("content.txt");
			Scanner input = new Scanner(inputFile);
			int count = 0;
			String[] words = null;
			while (input.hasNext()) {
				String line = input.nextLine();
				words = line.split(" ");
				count = words.length;
				System.out.println("  Word count: " + count + " Line is: " + line);
			}
			input.close();
		} catch (IOException e) {
			System.out.println("File not found: " + e.getMessage());
		}

	}
}




Sample Output (d): prints only those lines which have even number of words.

View Output

Solution for (d): prints only those lines which have even number of words.

View Solution

EvenTextReadRunner.java


import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class EvenTextReadRunner {

	public static void main(String args[]) {
		try {
			File inputFile = new File("content.txt");
			Scanner input = new Scanner(inputFile);
			int count=0;
			String[] words= null;
			while (input.hasNext()) {
				String line = input.nextLine();
				words = line.split(" ");
				count = words.length;
				if (count %2 == 0)
				System.out.println("Word count: "+ count + " Line is: " +line);
			}
			input.close();
		}
		catch (IOException e) {
			System.out.println("File not found: " + e.getMessage());
		}

	}
}





Java project files (with input files):


<-- Back to AP CSA Exam Solutions Next to Characters and Alphabets Count -->