【发布时间】:2020-03-16 17:30:34
【问题描述】:
我们正在尝试更正 TextArea 中单词的拼写
我们尝试了两种方法 onRemoveTwo 失败,出现 IndexOutOfBoundsException
另一种方法 onRemove 有效,但它做了一个 replaceAll 我们在代码中使用 replace
以下是两种方法的结果
使用 onRemoveTwo 的结果
初始文本 Take = Cariage NOT ME Carriag 将缺失的 voel 添加到 Carriage
要求更正“Cariage”
第一次校正结果 Take = Carriage NOT ME Carriag 将缺失的 voel 添加到 Carriage
With sb = sb.replace(from, to);
要求更正“Carriag”
第二次校正结果 Take = Carriagee NOT MEg 将缺失的 voel 添加到 Carriage
我们有这个错误原因:java.lang.IndexOutOfBoundsException
由我们理解的这行代码引起的
while 正在查找单词的两次出现
txaInput.replaceText(match.start(),match.end(),txtReplacementWord.getText());
使用 onRemove 的结果
初始文本 Take = Cariage NOT ME Carriag 将缺失的 voel 添加到 Carriage
要求更正“Cariage”
第一次校正结果 Take = Carriage NOT ME Carriag 将缺失的 voel 添加到 Carriage
要求更正“Carriag”
第二次修正结果 Take = Carriagee NOT ME Carriage 将缺失的 voel 添加到 Carriagee
请注意,“Carriage”都已更改为“Carriagee”
所以我们的问题是如何更具体地修正要纠正的词?
private void onRemoveTwo(){
if(txtReplacementWord.getText().isEmpty()){
txtMessage.setText("No Replacement Word");
return;
}
cboMisspelledWord.getItems().remove(txtWordToReplace.getText());
// Line Above Removes misspelled word from cboSelect
// ==================================================
String text = txaInput.getText();
String wordToFind = txtWordToReplace.getText();
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(text);
while(match.find()){
///System.out.println("Found "+word+" "+ match.start() +" - "+ (match.end()-1));
String from = word.toString();
String to = txtReplacementWord.getText();
String sb = txaInput.getText();
sb = sb.replace(from, to);
txaInput.replaceText(match.start(),match.end(),txtReplacementWord.getText());
txtMessage.setText("");
txtReplacementWord.setText("");
txtWordToReplace.setText("");
cboCorrectSpelling.getItems().clear();
cboMisspelledWord.requestFocus();
// Code above replaces misspelled word with correct spelling in TextArea
// =====================================================================
int SIZE = cboMisspelledWord.getItems().size();
if(SIZE == 0){
onCheckSpelling();
}
}
}
可行的方法,但会更改多个单词
@FXML
private void onReplace(){
if(txtReplacementWord.getText().isEmpty()){
txtMessage.setText("No Replacement Word");
return;
}
cboMisspelledWord.getItems().remove(txtWordToReplace.getText());
// Line Above Removes misspelled word from cboSelect
// ==================================================
String from = txtWordToReplace.getText();
String to = txtReplacementWord.getText();
String sb = txaInput.getText();
sb = sb.replace(from, to);
//sb = sb.replaceAll(from,to);
txaInput.setText("");
txaInput.setText(sb);
txtMessage.setText("");
txtReplacementWord.setText("");
txtWordToReplace.setText("");
cboCorrectSpelling.getItems().clear();
cboMisspelledWord.requestFocus();
// Code above replaces misspelled word with correct spelling in TextArea
// =====================================================================
int SIZE = cboMisspelledWord.getItems().size();
if(SIZE == 0){
onCheckSpelling();
}
}
【问题讨论】:
标签: javafx textarea stringbuilder