Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IndexBuilder is a class that takes a source file and the array of words to index

ID: 3640692 • Letter: I

Question

IndexBuilder is a class that takes a source file and the array of words to index and finds all of the occurrences of each word in the file. Write a IndexBuilder that takes a file object named sourceFile and a String[] object named indexWords as constructor arguments.

*The constructor should read the file line-by-line. As it reads each line, it should collect the following statistics:

*The total number of words and lines in the file.
* The lines where each index word appears - ignoring case and accounting for lines where an index word may appear more than once.

*You should only have to read each file once if you are doing this program properly.

Explanation / Answer

please rate import java.io.File; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class IndexBuilder { int no_of_lines_in_file = 0; int no_of_words = 0; List lines_with_index_words = new LinkedList(); public IndexBuilder(File sourceFile,String[] indexWords){ try{ Scanner getLine = new Scanner(sourceFile); while(getLine.hasNextLine()){ int flag=0; no_of_lines_in_file ++; String line = getLine.nextLine(); Scanner getWord = new Scanner(line); while(getWord.hasNext()){ no_of_words ++; String word = getWord.next(); for(int i=0;i