Count Lines and Words in Text Files - Free Response Practice Question (with solution)
| <-- Back to AP CSA Exam Solutions | Next to Characters and Alphabets Count --> |
Problem Statement
Write a Java program that reads text from a file, named content.txt and:
- counts and prints the number of lines in it.
- counts and prints the number of words in it.
- counts and prints the number of words in each line.
- 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 SolutionCountLinesRunner.java
The program uses the nextLine() method to read every line from the file. Next, it uses this method inside a loop to advance through each line (without storing it), and increments a counter on every iteration. When the loop ends, the counter will equal the total number of lines in the file.
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 SolutionCountWordsRunner.java
The program splits each line by a single space using split(" ") method. This splits the entire line word-by-word and then store them in an array. The length of the resulting array will equal the number of words on that line. This length is added to a running total. The accumulated total after all lines is the overall word count.
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 SolutionWordCountTextRunner.java
The program splits each line by a single space using split(" ") method. This splitss the entire line word-by-word and then store them in an array. The length of the resulting array (stored in count) will equal the number of words on that line. Both the count and the full line text can be printed together before moving to the next line.
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 SolutionEvenTextReadRunner.java
After splitting each line by space to get the word count, the program checks for lines with even word count using the following condition: (count % 2 == 0) and then print the line only when the remainder is zero; thus filtering out all lines with odd number of words.
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):
- content.txt
- CountLinesRunner.java
- CountWordsRunner.java
- WordCountTextRunner.java
- EvenTextReadRunner.java
| <-- Back to AP CSA Exam Solutions | Next to Characters and Alphabets Count --> |