Question

In: Computer Science

A text file called coit20245.txt consists of N lines each containing a name and a family...

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.

Solutions

Expert Solution

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


Related Solutions

Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Please submit SQL statements as a plain text file (.txt). If blackboard rejects txt file you...
Please submit SQL statements as a plain text file (.txt). If blackboard rejects txt file you can submit a zipped file containing the text file. Word, PDF, or Image format are not accepted. You do not need to show screen shot. Make sure you have tested your SQL statements in Oracle 11g. Problem 1. Please create the following tables for a tool rental database with appropriate primary keys & foreign keys. [30 points] Assumptions: Each tool belongs to a category....
Please submit SQL statements as a plain text file (.txt). If blackboard rejects txt file you...
Please submit SQL statements as a plain text file (.txt). If blackboard rejects txt file you can submit a zipped file containing the text file. Word, PDF, or Image format are not accepted. You do not need to show screen shot. Make sure you have tested your SQL statements in Oracle 11g. The list of tables is: Tables: Cust Table: cid, -- customer id cname, --- customer name cphone, --- customer phone cemail, --- customer email Category table: ctid, ---...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Allow the main process to generate a text file containing the text of this assignment. The...
Allow the main process to generate a text file containing the text of this assignment. The main process will then create two other processes and pass to them the name of the file and two codes (code1 and code2) using a pipe() system call. The codes are two different alphabetic letters such as “a” and “k”. The child processes should search the file for the character in the interval received, compute their frequencies and return them through a separate pipe...
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT