在这个类中,它加载了我的dictionary.txt文件,该文件包含正确拼写的字符串数组。然后它使用JFileChooser允许您选择txt文件进行拼写检查。它在字典文件中搜索spellCheck文件中的每个单词,如果找不到,则假定拼写错误,我必须在控制台中打印出拼写错误的单词。它打印出我的spellCheck.txt文件的每个单词,而不是那些拼写不正确的单词。我相信它与我的binarySearch方法有关。这是代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class SpellCheck {
private static String filePath, word;
private static String[] check, dictionary;
private static final int GROWTH = 2;
private static int checkIndex, dictIndex;
public static void loadDictionary(){
dictionary = new String[1000];
File dict = new File("dictionary.txt");
Scanner dictScanner;
dictIndex = 0;
try {
dictScanner = new Scanner(dict);
String word = dictScanner.nextLine().trim();
while(dictScanner.hasNextLine()){
word = dictionary[dictIndex];
dictIndex++;
word = dictScanner.nextLine().trim();
System.out.println(word);
if(dictIndex >= dictionary.length -1){
dictionary = resizeArray(dictionary);
}
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void readCheckFile(){
try{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == 0){
filePath = chooser.getSelectedFile().getAbsolutePath();
}else{
return;
}
FileReader spellCheck = new FileReader(filePath);
BufferedReader reader = new BufferedReader(spellCheck);
check = new String[1000];
checkIndex = 0;
word = "";
int charAsInt = reader.read();
while(charAsInt != -1){
char ch = (char)charAsInt;
if(Character.isAlphabetic(ch)){
word += ch;
}else{
if(word.length() > 1 && Character.isLowerCase(word.charAt(0))){
addWordToCheck(word);
}
word = "";
}
charAsInt = reader.read();
}
reader.close();
}catch(FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void addWordToCheck(String str) {
if(checkIndex >= check.length-1){
resizeArray(check);
}
check[checkIndex] = str;
if(!binarySearch(dictionary, str, 0, dictionary.length-1)){
System.out.println(str);
}
checkIndex++;
}
public static String[] resizeArray(String[] arr){
int newLength = arr.length * GROWTH;
arr = Arrays.copyOf(arr, newLength);
return arr;
}
private static boolean binarySearch(String[] sortedArray, String target, int start, int end){
if(start <= end){
int mid = (start + end)/2;
if(sortedArray[mid] == null){
return binarySearch(sortedArray, target, start, mid - 1);
}
if(sortedArray[mid].equals(target)){
return true;
}
else if (target.compareTo(sortedArray[mid]) > 0){
return binarySearch(sortedArray, target, start, mid - 1);
}
else{
return binarySearch(sortedArray, target, mid + 1, end);
}
}
return false;
}
}
答案 0 :(得分:0)
你不要把字典词放在任何地方。我想
word = dictionary[dictIndex];
必须是
dictionary[dictIndex]=word;
答案 1 :(得分:0)
有两个问题。首先你的话没有进入dictionary
数组。检查代码的这一部分:
String word = dictScanner.nextLine().trim();
while(dictScanner.hasNextLine()){
word = dictionary[dictIndex]; //replaces word with null
dictIndex++;
word = dictScanner.nextLine().trim();
//...
}
这里的一个侧面问题是你的第一个nextLine()
不安全。所以代码可能看起来像:
String word;
while(dictScanner.hasNextLine()){
word = dictScanner.nextLine().trim(); //sure to have next
dictionary[dictIndex] = word; //stores word in dictionary
dictIndex++;
//...
}
第二个问题出在您的binarySearch()
,您的compareTo
出错了。当对象按字典顺序排在参数之前时,String.compareTo()
会返回否定。