我想要做的是從最終用戶接收輸入並將其寫入名爲dictionary.txt的文件。然後我試圖檢查由最終用戶輸入的單詞是否已經在dictionary.txt文件中,因爲我不想重複。如果單詞不在字典中,則它將存儲在dictionary.txt中。我遇到的問題是compilation error
,它告訴我那Project3.java:81: not a statement FileWriter aDictionary = new FileWriter("dictionary.txt", true);
。小錯誤我不知道如何解決,「不是一個說法」
public static void addNewWord()throws IOException
{
String newWord = "";
newWord = JOptionPane.showInputDialog(null, "Enter the word you want to add to your dictionary.");
String aLineFromFile = "";
String filename = "dictionary.txt";
ArrayList<String> dictionary = new ArrayList<String>();
File resultsFile = new File(filename);
Scanner inFile = new Scanner(resultsFile);
while(inFile.hasNext())
aLineFromFile = inFile.nextLine();
if (aLineFromFile.startsWith(newWord))
{
dictionary.add(aLineFromFile);
JOptionPane.showMessageDialog(null, "Word already is dictionary");
addNewWord();
}
else
FileWriter aDictionary = new FileWriter("dictionary.txt", true);
PrintWriter out = new PrintWriter(aDictionary);
out.println(newWord);
out.close();
aDictionary.close();
return;
}
我已經進口java.io.*; javax.swing.*; java.awt.Dimension; java.util.*;
如果有人能告訴我爲什麼我收到此錯誤,並告訴我如何解決它,我將不勝感激。
你錯過了「else」之後的「{」。 –
之後檢查while循環大括號 – 2013-04-02 11:30:35
爲更好的幫助,儘快發佈[SSCCE](http://sscce.org/),短期運行,可編譯,生成異常 – mKorbel