Write and test a Java project that satisfies the following r

Write and test a Java project that satisfies the following requirements:

Searching for a string of characters in a body of text is a common application of computers.

Overview- find a “word” in a text file. Report how many times the word appears in the text file and on which lines.

More specifics: Your Java project will ask the user to enter the entire the path and name of a text file and ask the user for a word to search for. Your project will report on the screen how many times the word appears in the text and the line numbers where the word is found. For simplicity, case will NOT matter. That means if the user is searching for the word, “answer”, any hits on this word, such as “Answer”, “answer”, ANSWER” count as a match. If you give the user the option of whether case matters, that will earn 10 points more extra credit.

The Java API for the String class and the StringBuilder class have implemented this in some methods such as the indexOf( ) method. However, you are doing this exercise as a Computer Science exercise and are not allowed to use these API methods. The only methods of the String or StringBuilder class you are allowed to use are the length( ) method the charAt( ) method, the toUpperCase( ) method and the toLowerCase( ) method. If you use the other methods you will earn a zero on this extra credit test.

Write this project using the object-oriented paradigm. The main(   ) method will only prompt the user and read input, call the methods of the other class, and print the output to the screen.

The code must have at least two classes, the Driver and another class that does all the searching.

The code must give correct results in the output.

The code cannot use the methods of the String or StringBuilder classes except for the length( ) and charAt( ) and toUpperCase( ) and toLowerCase( ).

Solution

Please follow the code and comments for description :

CODE :

import java.util.Scanner; // required imports
import java.io.*;
import java.util.HashSet;

public class countWordsOccurrences { // class to run the code

    public static void main(String[] args) throws IOException { // driver method
        Scanner sc = new Scanner(System.in); // scanner class to read the data
        int count = 0, lineCount = 0; // required local variables
        HashSet set = new HashSet(); // set to set the line numbers

        System.out.print(\"Please Enter the Name of the Text File (including the path) : \"); // prompt for the user
        String name = sc.nextLine(); // get the line data
        File file = new File(name); // file object for the text file

        System.out.print(\"Please Enter the Word that you want to search for in the Text File : \"); // message
        String word = sc.nextLine(); // get the data
        word = word.trim(); // trim the data

        Scanner scanner = new Scanner(file); // scanner class to read the data

        while (scanner.hasNextLine()) { // read the data
            lineCount++; // increment the count of lines
            String line = scanner.nextLine(); // get the data
            Scanner lineScanner = new Scanner(line); // create an object
            while (lineScanner.hasNext()) { // read the word in the line
                String nextWord = lineScanner.next().trim(); // get the data
                if (nextWord.equalsIgnoreCase(word)) { // check for the words equality of ignorecase
                    ++count; // increment the count
                    set.add(lineCount); // if found add the line number to the set
                }
            }
        }
        System.out.println(\"The Word \\\"\" + word + \"\\\" appeared \" + count + \" times in the file \" + file); // print the data
        System.out.println(\"At line Numbers : \" + set.toString()); // get the line numbers
    }
}


OUTPUT :

Please Enter the Name of the Text File (including the path) : input.txt
Please Enter the Word that you want to search for in the Text File : the
The Word \"the\" appeared 11 times in the file input.txt
At line Numbers : [1, 5, 6, 7, 8, 9, 10]

input.txt :

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall
not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.


Hope this is helpful.

Write and test a Java project that satisfies the following requirements: Searching for a string of characters in a body of text is a common application of compu
Write and test a Java project that satisfies the following requirements: Searching for a string of characters in a body of text is a common application of compu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site