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

How do I write a java search and replace word program without using Arrays, gene

ID: 3621269 • Letter: H

Question

How do I write a java search and replace word program without using Arrays, generics, or imports other than java.util.Scanner and java.io.* using some kind of balanced binary tree?

The program reads an input text file, then perform a series of replacements, and output the result of these replacements. It must be executed using the following command:

java wordReplace input_file file_after_replacements

This will be followed by a sequence of commands coming from standard input (the console) each having the form

R search_word replacement_word

The program responds by writing to the console, not the output file, a list of the lines in which any replacement took place, giving both a before and an after version, the before version preceded by < and the after by > (leave a space between the angle bracket and the text).The special command Q will terminate program and cause the output file to be written.

The words can be stored in a balanced binary tree with a list of references to the lines in which they occur. For input, the program can read one line at a time using a Scanner and then define a Scanner for the line to get one word at a time:
Scanner lineScanner = new Scanner( line );
lineScanner.useDelimiter( "[\W]+" );

You can use the String method

replaceAll( "\b" + Word + "\b", "Replacement" )

to replace every occurrence of the word in a string (i.e. a line).

I know how to do this with generics and arrays, but how would I do it without?


Explanation / Answer

Dear.. Am giving the sample snippet about this program in java.. import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.Hashtable; import java.util.Enumeration; import java.lang.StringBuffer; public class MultipleSearch { private Pattern wordsToReplacePattern; private Hashtable replacementsTable; public MultipleSearch(Hashtable ht) { StringBuffer regexbuf = new StringBuffer(ht.size()*7); Enumeration wordsToReplace = ht.keys(); if (wordsToReplace.hasMoreElements()) { regexbuf.append(wordsToReplace.nextElement()); } while (wordsToReplace.hasMoreElements()) { regexbuf.append("|" + wordsToReplace.nextElement()); } wordsToReplacePattern = Pattern.compile(regexbuf.toString()); replacementsTable = ht; } public String replaceAll(String input) { StringBuffer sb = new StringBuffer(input.length()); Matcher m = wordsToReplacePattern.matcher(input); int end = 0; while (m.find()) { String wordToReplace = m.group(); String replacement = (String)replacementsTable.get(wordToReplace); m.appendReplacement(sb, replacement); end = m.end(); } sb.append(input.substring(end)); return sb.toString(); } public static void main(String args[]) { String s = "The quick brown fox jumped over the lazy dog's back."; Hashtable ht = new Hashtable(10); ht.put("quick", "slow"); ht.put("jump", "walk"); ht.put("lazy", "hard working"); ht.put("brown", "red"); MultipleSearch ms = new MultipleSearch(ht); System.out.println(ms.replaceAll(s)); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote