ReplaceInFile corrects a word everywhere in a text file. The input gives the word to replace (line 1), the new word (line 2) and then the text. The program must:
- write the text into the file
letter.txtwith aPrintWriter; - call
replaceInFile(File source, File target, String oldWord, String newWord), which readsletter.txtline by line with aScanner, writes the corrected lines intoletter-corrected.txtand returns the number of replacements; - read
letter-corrected.txtback and print it with line numbers, followed by a summary; - delete both files in a
finallyblock.
Also write occurrences(String line, String word) with indexOf. For the input
teh
the
Dear students, teh lab starts at nine.
Please bring teh handbook and teh laptop.
See you there.
the program prints
1: Dear students, the lab starts at nine.
2: Please bring the handbook and the laptop.
3: See you there.
Replaced "teh" by "the" 3 time(s) in 3 line(s)