Write and test a Java project that satisfies the following requirements: Searchi
ID: 3777533 • Letter: W
Question
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. I am sure you recall using ctrl-f to find a specific word in a document. Ways to implement this problem has been of great interest in the study of computer science algorithms.
In this assignment you are going to implement a brute-force approach to this problem. This is not the most efficient solution.
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. Give the user the option of whether case matters.
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 compile and run without modification.
The code must be well documented. Document each method and use the conventions for naming fields, constants, local variables, constants and class names. Use javadoc style comments above each method as well as inline comments.
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( ).
Explanation / Answer
Hi,
Please see below Classes for word search. Please comment for any quesries/feedbacks.
Thanks,
Anita
FileReaderDriver.java
import java.util.Scanner;
/**
* Class FileReaderDriver
*
*/
public class FileReaderDriver {
/**
*Main method
*
*/
public static void main(String[] args)
{
String fileName="";
String word="";
Scanner scan= new Scanner(System.in);
System.out.println("Enter the file name");
fileName = scan.nextLine();
System.out.println("Enter the word to search");
word = scan.nextLine();
//Calling FileRead Class method to search the word
FileRead fileRead= new FileRead();
fileRead.searchWord(fileName,word);
}
}
FileRead.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* Class FileRead
*
*/
public class FileRead{
/**
*This method search for a particular word in the given file and display the
*count and line numbers
*@input FileReader file, String word
*@output void
*/
public void searchWord(String file, String wordToSearch){
int count = 0,countBuffer=0,countLine=0;
String lineNumber = "";
BufferedReader br;
String line = "";
try {
//Creating BufferedReader to read the lines from File
br = new BufferedReader(new FileReader(file));
try {
//Reading each line from the file
while((line = br.readLine()) != null)
{
countLine++;
String[] words = line.split(" ");
//Checking id the words in current line contain the wordToSearch
for (String word : words) {
if (word.equalsIgnoreCase(wordToSearch)) {
//If wordToSearch present in current line, increment the counter
count++;
countBuffer++;
}
}
//If wordToSearch found multiple times in the current line,put the line Number only once
if(countBuffer > 0)
{
countBuffer = 0;
lineNumber += countLine + ",";
}
}
//Closing the BufferedReader
br.close();
} catch (IOException e) {
System.out.println("Exception.. "+e.getMessage());
}
} catch (FileNotFoundException e) {
System.out.println("Exception.. "+e.getMessage());
}
//Display no Of Times
System.out.println("Times found at: "+count);
//Display lineNumbers
System.out.println("Word found at: "+ lineNumber);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.