我正在學習 Mooc.fi Java 課程,如果檔案已經包含它,我不知道如何不將字串寫入檔案。我只嘗試了一個字串,嘗試了沒有“”(空白),也沒有另一個字串,但即使檔案已經包含它,它仍然會添加字串。
并且translate()方法不會找到/回傳找到給定單詞的整行。
public class main {
public static void main(String[] args) throws Exception {
MindfulDictionary dict = new MindfulDictionary();
dict.add("apina", "monkey");
dict.add("banaani", "banana");
dict.add("apina", "apfe");
System.out.println( dict.translate("apina") );
System.out.println( dict.translate("monkey") );
System.out.println( dict.translate("programming") );
System.out.println( dict.translate("banana") );
}
}
public class MindfulDictionary {
File file;
FileWriter writer;
Scanner imeskanera;
public MindfulDictionary() throws Exception {
this.file = new File("C:\\Users\\USER\\Desktop\\test.txt");
this.imeskanera = new Scanner(this.file, "UTF-8");
}
public void add(String word, String translation) throws Exception {
boolean found = false;
while(this.imeskanera.hasNextLine()) {
String lineFromFile = this.imeskanera.nextLine();
if(word.contains(lineFromFile)) {
found = true;
break;
}
}
if(!found) {
this.writer = new FileWriter(this.file,true);
this.writer.write(word " " translation "\n");
this.writer.close();
}
}
public String translate(String word) throws Exception {
String line = null;
while(this.imeskanera.hasNextLine()) {
String data = this.imeskanera.nextLine();
if(data.contains(word)) {
line = data;
break;
}
}
return line;
}
}
uj5u.com熱心網友回復:
問題是您的Scanner物件已被該add()方法使用。您需要重新打開輸入流才能讀取檔案的內容。如果你添加
this.imeskanera = new Scanner(this.file, "UTF-8");
在translate()方法的開頭,它應該是單詞。這基本上告訴你沒有必要Scanner成為一個全球性的領域。在每種方法中本地使用它。這就是我過去解釋檔案流概念的方式:
從邏輯上考慮檔案流(用于讀取和寫入)。您不能允許這樣的流是“回圈的”。否則,當您嘗試獲取“下一行”時,總會有下一行,您將永遠無法停止讀取(或寫入)。流在到達末尾時被消耗,一旦完成,要回到流的開頭,您需要打開一個新的;不重復使用舊的。
我認為即使在答案被接受后我也需要添加這個解釋,因為我知道新開發人員會為這個概念而苦苦掙扎,因此,有必要詳細解釋它。
話雖如此,您的MindfulDictionary課程應該如下所示:
public class MindfulDictionary {
File file;
FileWriter writer;
// Scanner imeskanera;
public MindfulDictionary() throws Exception {
this.file = new File("test.txt"); // I changed the path to the file to make it work for me. You can change it back if you want to.
file.createNewFile();
}
public void add(String word, String translation) throws Exception {
Scanner imeskanera = new Scanner(this.file, "UTF-8");
boolean found = false;
while (imeskanera.hasNextLine()) {
String lineFromFile = imeskanera.nextLine();
if (word.contains(lineFromFile)) {
found = true;
break;
}
}
if (!found) {
this.writer = new FileWriter(this.file, true);
this.writer.write(word " " translation "\n");
this.writer.close();
}
imeskanera.close();
}
public String translate(String word) throws Exception {
Scanner imeskanera = new Scanner(this.file, "UTF-8");
String line = null;
while (imeskanera.hasNextLine()) {
String data = imeskanera.nextLine();
if (data.contains(word)) {
line = data;
break;
}
}
imeskanera.close();
return line;
}
}
我用我的修改運行了你的代碼,現在輸出是
apina monkey
apina monkey
null
banaani banana
uj5u.com熱心網友回復:
除了@hfontanez 的回答提到的掃描儀問題之外,還有以下變化。
if (word.contains(lineFromFile))
這會檢查第一個單詞是否包含該行,這是不正確的。該檔案包含第一個單詞和翻譯。所以這可以改為
if (lineFromFile.contains(word))
正如@ghostCat 提到的,搜索關鍵字(單詞)可以重構。帶有這些更改的代碼。
public class MindfulDictionary {
File file;
FileWriter writer;
// Scanner imeskanera;
public MindfulDictionary() throws Exception {
this.file = new File("test.txt");
file.createNewFile();
}
public void add(String word, String translation) throws Exception {
if (get(word) == null) {
this.writer = new FileWriter(this.file, true);
this.writer.write(word " " translation "\n");
System.out.println("Out>>:" word " " translation "\n");
this.writer.close();
}
}
private String get(String word) throws Exception {
Scanner imeskanera = new Scanner(this.file, "UTF-8");
boolean found = false;
String retStr= null;
while (imeskanera.hasNextLine()) {
String lineFromFile = imeskanera.nextLine();
if (lineFromFile.contains(word)) {
found = true;
retStr=lineFromFile;
break;
}
}
imeskanera.close();
return(retStr);
}
public String translate(String word) throws Exception {
return get(word);
}
public static void main(String[] args) throws Exception {
MindfulDictionary dict = new MindfulDictionary();
dict.add("apina", "monkey");
dict.add("apina", "monkey");
dict.add("banaani", "banana");
dict.add("apina", "apfe");
System.out.println( dict.translate("apina") );
System.out.println( dict.translate("monkey") );
System.out.println( dict.translate("programming") );
System.out.println( dict.translate("banana") );
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/413553.html
標籤:
上一篇:以太坊礦池中轉服務器
