In: Computer Science
A text file called coit20245.txt consists of N lines each containing a name and a family name as follows. N is largest digit of your student id number. Example of coit20245.txt file: Daniel Atkinson Rob Jackson Brian Lara Write a main() method that reads the rows of coit20245.txt and counts the lines which starts with a first letter of your name. You are to provide class comments that describe what the application does. You are also to provide comments that describe what each significant block of code does.
The source code for the program is given below. SInce no
specific programming language was mentioned, the source code is
provided in Java.
Please refer to the comments in the code to understand the working
of program.
Main.java
/*
The program reads the file "coit20245.txt" which contains names of people and it prints the count
of the number of names that start with the first character of user's name
*/
import java.io.*;
public class Main {
public static void main(String[] args) {
// declaring the variables to be used in the program.
// this will store the complete name as ["firstname", "lastname"]
String[] full_name = new String[2];
char myNameStartsWith = 'a'; // this is the first character of user's name
int count = 0; // this is the count of names which match the criteria
String line; // this stores a single line read at a time from the file
int N = 8; // largest digit of student id
/*
* when dealing with files, we need to consider exceptions and handle them
* gracefully. Therefore we are using try-catch
*/
try {
// first we create a file instance to read from. Java's File Class is used for
// this task.
File namesFile = new File("coit20245.txt");
/*
* FileReader class in java is used to deal with character streams so we create
* an object of FileReader class and pass it's constructor the instance of file
* created in previous step.
*/
FileReader fr = new FileReader(namesFile);
/*
* The BufferedReader class reads the text from the input stream(can be the
* console or the file as in this program)
*/
BufferedReader br = new BufferedReader(fr);
/*
* Now we start reading the file.
*
* The readLine() method of BufferedReader class reads the file line-by-line and
* stores each line in the line variable.
*
* Then we split the line based on space and store the individual words in the
* full_name variable such that full_name[0] = 'firstname' and full_name[1] =
* 'lastname'
*
* Then we check if the first character of the name read from the file matches
* the first character of user's name or not if it matches we increment the
* count otherwise we continue to the next name
*
*/
// start reading the file till end of file is reached
for(int i = 1; i <= N; i++) {
line = br.readLine();
full_name = line.split(" ");
/*
* note that we use the toLowerCase() method to avoid the case sensitivity of
* characters
*/
if (full_name[0].toLowerCase().startsWith(String.valueOf(myNameStartsWith))) {
count++;
}
}
fr.close(); // close the file reader to prevent resource leak
System.out.println(count + " name(s) found."); // print the count of matching names found
} catch (IOException ex) {
// in case any exception occurred, print the stack trace
ex.printStackTrace();
}
}
}
Please refer to the screenshot of code below to understand the
indentation of the program.
The contents of coit20245.txt file are shown below:
OUTPUT