因此,我想创建一个文本编辑器程序,通过命令完成编辑,而命令又由相应的方法执行。我想用这个文本编辑器程序有两个文件a)一个新的.txt文件,比如两个城市的故事,b)一个包含正确拼写单词列表的文件,作为拼写检查方法的参考 我是利用以下内容:
这是我到目前为止的代码。它汇编。但是,当我尝试对任何方法进行对象测试时,它们将无法工作。例如,我在.txt文件中查找字符串出现的方法将始终返回false我假设存在一些问题a)我的布尔方法循环b)创建我的链表有一些问题我用于我正在读入的文件和/或我用于我的字典/参考文件的哈希映射。我很难过。
package FileEditor;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
public class FileEditor {
static LinkedList<String> list = new LinkedList<String>();
public FileEditor() {
super();
}
public static void readNovelFile() {
String content = new String();
File file = new File("2city10.txt");
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()) {
content = sc.nextLine();
list.add(content);
}
sc.close();
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
System.out.println("\nProgram terminated Safely...");
}
}
public static boolean findText(String x) {
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x) || text.equals(x)) {
return true;
} else {
return false;
}
}
return false;
}
public static void findAndReplace(String x, String y) {
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x) || text.equals(x)) {
text = text.replaceAll(x, y);
list.remove(i);
list.add(i, text);
}
}
}
public static void findAndInsert(String x, String y) {
boolean flag = false;
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x) || text.equals(x)) {
if (flag == false)
text = text.replace(x, x + " " + y);
list.remove(i);
list.add(i, text);
}
flag = true;
}
}
public static void delete(String x) {
boolean flag = false;
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x) || text.equals(x)) {
if (flag == false)
text = text.replace(x, "");
list.remove(i);
list.add(i, text);
}
flag = true;
}
}
public static HashSet<String> readWords(String filename) throws FileNotFoundException {
HashSet<String> words = new HashSet<String>();
Scanner in = new Scanner(new File(filename));
// Use any characters other than a-z or A-Z as delimiters
in.useDelimiter("[^a-zA-Z]+");
while (in.hasNext()) {
words.add(in.next().toLowerCase());
}
return words;
}
public static void spellCheck() {
// Read the dictionary and the document
Set<String> dictionaryWords = null;
Set<String> documentWords = null;
boolean flag = false;
try {
dictionaryWords = readWords("EnglishWordList.txt");
documentWords = readWords("2city10.txt");
} catch (FileNotFoundException e) {
}
// Print all words that are in the document but not the dictionary
for (String word : documentWords) {
if (!dictionaryWords.contains(word) && flag == false) {
System.out.println(word);
flag = true;
}
}
}
public static void spellCheckAll() {
// Read the dictionary and the document
Set<String> dictionaryWords = null;
Set<String> documentWords = null;
try {
dictionaryWords = readWords("EnglishWordList.txt");
documentWords = readWords("2city10.txt");
} catch (FileNotFoundException e) {
}
// Print all words that are in the document but not the dictionary
for (String word : documentWords) {
if (!dictionaryWords.contains(word)) {
System.out.println("Misspelled words :" + word);
}
}
}
public static void saveFile() {
BufferedWriter out;
try {
out = new BufferedWriter(new FileWriter("2city10.txt"));
for (int i = 0; i < list.size(); i++) {
out.write(list.get(i).toString());
out.write('\n'); // add a new line
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void printFile() {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
//out.write('\n'); // add a new line
}
}
public static void menuList() {
System.out.println("\n Enter the Choice ...");
System.out.println("\n Enter 1 to Find ");
System.out.println("\n Enter 2 to FindReplace ");
System.out.println("\n Enter 3 to FindInsert ");
System.out.println("\n Enter 4 to Delete ");
System.out.println("\n Enter 5 to spellCheck ");
System.out.println("\n Enter 6 to spellCheckAll ");
System.out.println("\n Enter 7 to save ");
System.out.println("\n Enter 8 to print ");
System.out.println("\n Enter 9 to quit ");
}
public static void main(String[] args) throws IOException {
readNovelFile();
int choice = 0;
menuList();
Scanner scanner = new Scanner(System.in);
choice = scanner.nextInt();
while (true) {
switch (choice) {
case 1:
{
String input = "";
System.out.println("\nEnter the string to Find ...");
Scanner textscan = new Scanner(System.in);
input = textscan.nextLine();
System.out.println("The String entered exists :" +
findText(input));
menuList();
choice = scanner.nextInt();
break;
}
case 2:
String find = "";
String replace = "";
System.out.println("\nEnter the string to Find ...");
Scanner findScan = new Scanner(System.in);
find = findScan.nextLine();
System.out.println("\nEnter the string to Replace ...");
Scanner replaceScan = new Scanner(System.in);
replace = replaceScan.nextLine();
findAndReplace(find, replace);
menuList();
choice = scanner.nextInt();
break;
case 3:
String findStr = "";
String insStr = "";
System.out.println("\nEnter the string to Find ...");
Scanner findStrScan = new Scanner(System.in);
findStr = findStrScan.nextLine();
System.out.println("\nEnter the string to Insert ...");
Scanner InsertStrScan = new Scanner(System.in);
insStr = InsertStrScan.nextLine();
findAndInsert(findStr, insStr);
menuList();
choice = scanner.nextInt();
break;
case 4:
String delete = "";
System.out.println("\nEnter the string to Delete ...");
Scanner deleteScan = new Scanner(System.in);
delete = deleteScan.nextLine();
delete(delete);
menuList();
choice = scanner.nextInt();
break;
case 5:
System.out.println("\nSpell checking for first occurence ....");
spellCheck();
menuList();
choice = scanner.nextInt();
break;
case 6:
System.out.println("\nSpell checking for All occurences ....");
spellCheckAll();
menuList();
choice = scanner.nextInt();
break;
case 7:
System.out.println("\nSaving the File ....");
saveFile();
menuList();
choice = scanner.nextInt();
break;
case 8:
System.out.println("\nSaving and Printing the File ....");
saveFile();
printFile();
menuList();
choice = scanner.nextInt();
break;
case 9:
System.out.println("\nEXIT MENU ....");
System.exit(0);
break;
}
}
}
}
答案 0 :(得分:0)
对于findText
总是返回错误....
public static boolean findText(String x) {
boolean found;
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x)) { // You also dont need that "equals()" here
found=true;
break; // Break loop if text found
} else {
found=false;
}
}
return found;
}
但正如评论中所提到的,更好的方法是:
public static boolean findText(String x) {
boolean found;
for (int i = 0; i < list.size(); i++) {
if(list.get(i).indexOf(x) >= 0){
found = true;
break;
}
else
found = false
}
return found;
}
在没有手动涉及循环的情况下执行此操作的另一种方法是:
public static boolean findText(String x) {
int index = Collections.binarySearch(list, x);
if(index >= 0)
return true;
return false;
}