BuildIndex is the class that reads the list of words to index from an input file
ID: 3640714 • Letter: B
Question
BuildIndex is the class that reads the list of words to index from an input file, collects occurrences of these words using IndexBuilder objects below, and outputs the final index. IndexBuilder is a class takes a source file and the array of words to index and finds all of the occurrences of each word in the file. The Index builder objects are: 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<indexWords.length; i++ ){
if(word.equalsIgnoreCase(indexWords[i])){
Write a BuildIndex class that should create a File object for each source file name given and check to see that the file actually exists. If the file does exist, the program should create an IndexBuilder object for that file, passing in the array of words to index.
Explanation / Answer
Following programs displays the Number of words, Lines and index of all the words in the file. Please rate.
It has two classes IndexBuilder and BuildIndex
BuildIndex instantiates IndexBuilder and it prints No of words, lines and index of all the words.
If the file is not found, in.txt file is created and the words, lines and index of words are displayed.
package words;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.List;
public class IndexBuilder {
int no_of_words;
int no_of_lines_in_file;
List lines_with_index_words;
public IndexBuilder() {
try {
Words();
Lines();
IndexOfAllWords();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getNoOfWords() {
return no_of_words;
}
public int getNoOfLines() {
return no_of_lines_in_file;
}
public List getLinesWithIndexWords() {
return lines_with_index_words;
}
public ArrayList<Integer> IndexOfAllWords() throws IOException {
LineNumberReader rdr = new LineNumberReader(new FileReader("in.txt"));
ArrayList<Integer> results = new ArrayList<Integer>();
try {
String line;
while ((line = rdr.readLine()) != null) {
String[] strarr = line.split(" ");
for (String str : strarr) {
if (line.indexOf(str) >= 0) {
results.add(rdr.getLineNumber());
System.out.println(str + " " + " occurs in Line "
+ rdr.getLineNumber());
}
}
}
} finally {
rdr.close();
}
return results;
}
public void Words() throws IOException // count no. of words.
{
File f = new File("in.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
StreamTokenizer stz = new StreamTokenizer(br);
int index = 0;
int numWords = 0;
while (index != StreamTokenizer.TT_EOF) {
index = stz.nextToken();
numWords++;
}
System.out.println("Number of words in in.txt " + numWords);
this.setNo_of_words(numWords);
}
public void Lines() throws IOException {
File f = new File("in.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
LineNumberReader ln = new LineNumberReader(br);
int count = 0;
while (ln.readLine() != null) {
count++;
}
System.out.println("Number of lines in in.txt " + count);
this.setNo_of_lines_in_file(count);
}
public int getNo_of_words() {
return this.no_of_words;
}
public void setNo_of_words(int noOfWords) {
this.no_of_words = noOfWords;
}
public int getNo_of_lines_in_file() {
return this.no_of_lines_in_file;
}
public void setNo_of_lines_in_file(int noOfLinesInFile) {
this.no_of_lines_in_file = noOfLinesInFile;
}
public List getLines_with_index_words() {
return this.lines_with_index_words;
}
public void setLines_with_index_words(List linesWithIndexWords) {
this.lines_with_index_words = linesWithIndexWords;
}
}
package words;
public class BuildIndex {
public static void main(String[] args) {
IndexBuilder inb = new IndexBuilder();
}
}
Ouput:
Number of words in in.txt 23
Number of lines in in.txt 2
The occurs in Line 1
file occurs in Line 1
is occurs in Line 1
written occurs in Line 1
from occurs in Line 1
the occurs in Line 1
program occurs in Line 1
occurs in Line 2
The occurs in Line 2
program occurs in Line 2
used occurs in Line 2
to occurs in Line 2
print occurs in Line 2
words, occurs in Line 2
lines occurs in Line 2
and occurs in Line 2
index occurs in Line 2
of occurs in Line 2
words occurs in Line 2
in occurs in Line 2
each occurs in Line 2
line. occurs in Line 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.