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


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




Analyze Student Data from Text Files

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


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


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


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



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 -->