Free Response Practice Question (with solution)- Count Characters and Alphabets in Text Files


<-- Back to Lines and Words Count Next to Analyzing Student Names -->




Count Characters and Alphabets in Text Files

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

  1. counts and prints the number of characters in it (including spaces).
  2. counts and prints the number of characters in it (excluding spaces).
  3. counts and prints the number of alphabets in each line.
  4. counts and prints the number of uppercase and lowercase alphabets in each line.

Sample Input: day.txt


Sample Output (a): counts and prints the number of characters in it (including spaces).

View Output


Solution for (a): counts and prints the number of characters in it (including spaces).

View Solution

CountAllCharactersRunner.java


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

public class CountAllCharactersRunner {

	public static void main(String args[]) {
		try {
			File inputFile = new File("day.txt");
			Scanner input = new Scanner(inputFile);
			int count = 0;

			while (input.hasNext()) {
				String line = input.nextLine();
				count = count + line.length();
			}
			System.out.println("Total Character count: " + count);
			input.close();

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

	}
}




Sample Output (b): counts and prints the number of characters in it (excluding spaces).

View Output

Solution for (b): counts and prints the number of characters in it (excluding spaces).

View Solution

CountNonSpaceCharRunner.java


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

public class CountNonSpaceCharRunner {

	public static void main(String args[]) {
		try {
			File inputFile = new File("day.txt");
			Scanner input = new Scanner(inputFile);
			int count = 0;
			String[] characters = null;
			while (input.hasNext()) {
				String line = input.nextLine();
				characters = line.split("");
				for (int i = 0; i < characters.length; i++) {
					if (!characters[i].equals(" "))
						count++;
				}

			}
			System.out.println("Total Non-Space Character count: " + count);
			input.close();

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

	}
}





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

View Output

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

View Solution

CountAlphabetsPerLineRunner.java


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

public class CountAlphabetsPerLineRunner {
	public static void main(String[] args) {

		try {
			File inputFile = new File("day.txt");
			Scanner input = new Scanner(inputFile);

			int lineNumber = 1;

			while (input.hasNextLine()) {
				String line = input.nextLine();
				int count = 0;
				for (int i = 0; i < line.length(); i++) {
					char ch = line.charAt(i);
					if (Character.isLetter(ch)) {
						count++;
					}
				}
				System.out.println(" Line " + lineNumber + ": " + count + " alphabets");
				lineNumber++;
			}
			input.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not found.");
		}
	}
}





Sample Output (d): counts and prints the number of uppercase and lowercase alphabets in each line.

View Output

Solution for (d): counts and prints the number of uppercase and lowercase alphabets in each line.

View Solution

CountCasePerLineRunner.java


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

public class CountCasePerLineRunner {
	public static void main(String[] args) {

		try {
			File inputFile = new File("day.txt");
			Scanner input = new Scanner(inputFile);

			int lineNumber = 1;
			while (input.hasNextLine()) {
				String line = input.nextLine();

				int upper = 0;
				int lower = 0;

				for (int i = 0; i < line.length(); i++) {
					char ch = line.charAt(i);
					if (Character.isLetter(ch)) {
						if (Character.isUpperCase(ch)) {
							upper++;
						} else if (Character.isLowerCase(ch)) {
							lower++;
						}
					}
				}
				System.out.println("Line " + lineNumber + ": Uppercase = " + upper + ", Lowercase = " + lower);
				lineNumber++;
			}

			input.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not found.");
		}
	}
}






Java project files (with input files):


<-- Back to Lines and Words Count Next to Analyzing Student Names -->