我的问题是在文本文件中查找和替换字符串。
此程序应在ghost
文本文件中查找单词A Christmas Carol
的实例,并将bunny
替换为ghost
,将Bunny
替换为Ghost
}和BUNNY
GHOST
。它适用于小写鬼,并且没有GHOST
的实例,但它似乎不适用于大写的Ghost
。
我知道我的问题属于最后两种方法。我做错了什么?
package findandreplace;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class FindAndReplace {
public static void main(String[] args) {
String filename = "24022-0.txt"; //Charles Dickens - A Christmas Carol
try {
Scanner fin = getScannerFromFile(filename);
String [] story = readFile(fin);
String [] newStory = new String[story.length];
for (int i = 0; i < story.length; i++) {
newStory[i] = substitute(story[i], "ghost", "bunny");
}
if (writeFile("bunny.txt", newStory))
System.out.println("New file successfully created.");
} catch (FileNotFoundException e) {
System.out.println("File not found");
System.exit(1);
}
}
private static Scanner getScannerFromFile(String filename) throws FileNotFoundException {
File f = new File(filename);
return new Scanner(f);
}
private static String [] readFile(Scanner fin) {
ArrayList<String> lines = new ArrayList<>();
while (fin.hasNext()) {
lines.add(fin.nextLine());
}
return lines.toArray(new String[0]);
}
private static boolean writeFile(String newfile, String [] lines) {
File file = new File(newfile);
if (file.exists()) {
System.out.println("File already exists");
}
try {
PrintWriter fout = new PrintWriter(file);
for (String line : lines) {
fout.println(line);
}
fout.close();
} catch (FileNotFoundException ex) {
System.err.println("File error.");
return false;
}
return true;
}
/**
* This method should examine the first word, and return the second word matching case.
* For example, if first word is "GHOST", and second word is "bunny", the return value
* should be "BUNNY".
* NOTE: THIS SHOULD *NOT* BE A RECURSIVE FUNCTION
* precondition: the first word is all lowercase, Capitalized, or ALL-CAPS
* @return second, matching case of first
*/
private static String matchWordCase(String first, String second) {
if (first.toUpperCase().equals(first)) {
second = second.toUpperCase();
}
if (first.toLowerCase().equals(first)) {
second = second.toLowerCase();
}
else if (first.toUpperCase().charAt(0) == first.charAt(0)) {
second = second.substring(0, 1).toUpperCase() + second.substring(1);
}
return second;
}
/**
* This method should return line, with every instance of the word find
* should be replaced by replace. There are multiple approaches you can take,
* but words should match regardless of case, and case should be preserved.
* If you want to assume words are space-separated, you may. However, it is
* better if you figure out how to do this otherwise.
* NOTE: THIS SHOULD BE A RECURSIVE FUNCTION
* @return line - with the appropriate substitutions
*/
private static String substitute(String line, String find, String replace) {
StringBuilder newLine = new StringBuilder();
//TODO:
String result;
if (line.length() < find.length()) {
return line;
}
if (find.equals(line.substring(0,find.length()))) {
line = matchWordCase(find, replace) +
substitute(line.substring(find.length()), find, matchWordCase(find,
replace));
} else {
result = line.charAt(0) + substitute(line.substring(1), find,
replace);
}
return result;
}
}
答案 0 :(得分:0)
在substitute
方法的if
语句中,条件是find.equalsIgnoreCase
,而不是find.equals
吗?