In the concordance example of Segment 19.18, if a word occurs more than once in
ID: 3839377 • Letter: I
Question
In the concordance example of Segment 19.18, if a word occurs more than once in a single line, the number of that line appears more than once in the concordance. Revise the method readFilegiven in Segment 19.20 so that the line numbers associated with a given word are distinct. (DR.JAVA) How do revise the method so the line numbers associated with a given word are distinct. (Book: Data Structures and Abstractions by Frank Carrano 4th Edition)
19.20 The method readFile. The method read File reads the text file and uses the dictionary wordTab to create the concordance. Since we must record the line number of each word, we read the file a li at a time. We process all the words in a line before moving on to the next line. Thus, the followin definition of readFile contains two loops that are nested. The outer loop uses the scanner. passed the method as an argument to read lines from the file. The inner loop uses another scanner to extra the words from a line as soon as it is read. The class LinkedListWithIterator from Segment 15. of Chapter 15 is used to form each list of line numbers public void readFile(Scanner data) int lineNumber 1; while data. hasNext C)) String line data.nextLine C); line line toLowerCase() Scanner line Processor new Scanner (line line Processor.useDelimiter WW+"); While Cline Processor.hasNexto) String next Word lineProcessor.nexto: Listwi thIteratorInterfaceExplanation / Answer
public void readFile(Scanner data)
{
int lineNumber=1;
while (data.hasNext())
{
String line=data.nextline();
line=line.toLowerCase();
Scanner lineProcessor= new Scanner(line);
lineProcessor.useDelimiter("\w+");
while (lineProcessor.hasNext())
{
String nextWord=lineProcessor.next();
String wordLine+= nextWord;
String dupWord;
int i=0;
while(wordLine != null ) //if required // && wordLine.length()
{
for(j=i+1; j<wordLine.length(); j++)
{
if(wordLine[i]== wordLine[j])
{
//if required //wordLine[j]=wordLine[i]+"["+String.valueOf(j)+"]";
nextWord+="["+String.valueOf(j)+"]";
break;
}
}
i++;
}
ListWithIteratorInterface<Integer> linelist= wordTable.getValue(nextWord);
if(lineList== null)
{
lineList=new ListWithIteratorInterface<Integer>();
wordTable.add(nextWord, lineList);
}
lineList.add(lineNumber);
}
}
}