Analyze Student Data from Text Files - Free Response Practice Question (with solution)


<-- Back to Characters and Alphabets Count Next to Interpreting River Data -->




Problem Statement

Write a Java program that reads the names of students from a text file, named names.txt and:

  1. prints the first character of the first name of each student.
  2. prints all the names in uppercase.
  3. counts and prints the number of vowels in each name.
  4. counts and prints all the consonant characters in each name.

Sample Input: names.txt


Sample Output (a): prints the first character of the first name of each student.

View Output


Solution for (a): prints the first character of the first name of each student.

View Solution

PrintFirstCharRunner.java

Each line read from the file is a student name, and charAt(0) retrieves its very first character. That single character is printed directly, giving the initial of each name.

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

public class PrintFirstCharRunner {

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

			while (input.hasNext()) {
				String line = input.nextLine();
				System.out.println(line.charAt(0));
			}
			input.close();

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

	}
}

Sample Output (b): prints all the names in uppercase.

View Output

Solution for (b): prints all the names in uppercase.

View Solution

PrintUpperCaseRunner.java

The program reads each name line and calls toUpperCase() on the string before printing it. This converts every lowercase letter in the name to its uppercase equivalent in a single step.

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

public class PrintUpperCaseRunner {

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

			while (input.hasNext()) {
				String line = input.nextLine();
				System.out.println(line.toUpperCase());
			}
			input.close();

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

	}
}


Sample Output (c): counts and prints the number of vowels in each name.

View Output

Solution for (c): counts and prints the number of vowels in each name.

View Solution

CountVowelsRunner.java

The name is first converted to lowercase with toLowerCase() so that only five characters need to be checked. The loop then uses charAt(i) to test each character against a, e, i, o, u and increments the counter on a match.

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

public class CountVowelsRunner {

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

			while (input.hasNext()) {
				int count = 0;
				String line = input.nextLine();
				String str = line.toLowerCase();

				for (int i = 0; i < str.length(); i++) {
					char ch = str.charAt(i);
					if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
						count++;
					}
				}
				System.out.println("Vowel Count: " + count + " Name: " + line);
			}

			input.close();

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

	}
}

Sample Output (d): counts and prints all the consonant characters in each name.

View Output

Solution for (d): counts and prints all the consonant characters in each name.

View Solution

CountNonVowelsRunner.java

The loop checks each character against the five vowels; any character that does not match falls into the else branch, where it is counted and appended to a consonant string. The count and the collected consonant characters are printed together for each name.

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

public class CountNonVowelsRunner {

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

			while (input.hasNext()) {
				int count = 0;
				String line = input.nextLine();
				String str = line.toLowerCase();
				String consonant = "";
				for (int i = 0; i < str.length(); i++) {
					char ch = str.charAt(i);
					if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

					} else {
						count++;
						consonant = consonant + line.charAt(i);
						;
					}
				}
				System.out.println("Consonant Count: " + count + " Consonant: " + consonant + " Name: " + line);
			}

			input.close();

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

	}
}

Java project files (with input files):


<-- Back to Characters and Alphabets Count Next to Interpreting River Data -->