简单Java文本编辑器程序不工作
因此,假设我创建了一个文本编辑器程序,其中编辑是通过一个命令来完成的,而这个命令又是由相应的方法来执行的。假设我使用这个文本编辑器程序,它有两个文件a)一个新的.txt文件,如双城记;b)一个包含正确拼写单词列表的文件,作为拼写检查方法的参考 我要做的是:简单Java文本编辑器程序不工作,java,Java,因此,假设我创建了一个文本编辑器程序,其中编辑是通过一个命令来完成的,而这个命令又是由相应的方法来执行的。假设我使用这个文本编辑器程序,它有两个文件a)一个新的.txt文件,如双城记;b)一个包含正确拼写单词列表的文件,作为拼写检查方法的参考 我要做的是: 布尔查找(字符串x)//在文件中查找单词“x”,如果找到则返回true,否则返回false 布尔值FindReplace(字符串x,字符串y)//查找文件中第一个出现的单词“x”,如果发现返回true,则将其替换为单词“y”,否则返回fals
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;
}
}
}
}
包文件编辑器;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.HashSet;
导入java.util.LinkedList;
导入java.util.Scanner;
导入java.util.Set;
公共类文件编辑器{
静态LinkedList=新建LinkedList();
公共文件编辑器(){
超级();
}
公共静态void readNovelFile(){
字符串内容=新字符串();
File File=新文件(“2city10.txt”);
试一试{
Scanner sc=新扫描仪(新文件输入流(文件));
while(sc.hasNextLine()){
content=sc.nextLine();
列表。添加(内容);
}
sc.close();
}捕获(FileNotFoundException fnf){
fnf.printStackTrace();
}捕获(例外e){
e、 printStackTrace();
System.out.println(“\n程序安全终止…”);
}
}
公共静态布尔findText(字符串x){
对于(int i=0;ipublic 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;
}