2013-04-02 51 views
-5

我想要做的是從最終用戶接收輸入並將其寫入名爲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.*; 如果有人能告訴我爲什麼我收到此錯誤,並告訴我如何解決它,我將不勝感激。

+2

你錯過了「else」之後的「{」。 –

+0

之後檢查while循環大括號 – 2013-04-02 11:30:35

+0

爲更好的幫助,儘快發佈[SSCCE](http://sscce.org/),短期運行,可編譯,生成異常 – mKorbel

回答

1

把別人的代碼inide這{}

這樣

else{ 
     FileWriter aDictionary = new FileWriter("dictionary.txt", true); 
     PrintWriter out = new PrintWriter(aDictionary); 
     out.println(newWord); 
     out.close(); 
     aDictionary.close(); 
     } 
3

不要忘記縮進很好,它有助於查看丟失的括號和範圍。

這應該更好地工作:以下修改後的代碼

public static void addNewWord() throws IOException { 
    String newWord = JOptionPane.showInputDialog(null, "Enter the word you want to add to your dictionary."); 
    String filename = "dictionary.txt"; 
    File resultsFile = new File(filename); 
    Scanner inFile = new Scanner(resultsFile); 
    List<String> dictionary = new ArrayList<String>(); 
    while (inFile.hasNext()) { 
     String 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(); 
     } 
    } 
} 
0

使用。你錯過了while循環後的括號和其他阻止

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(); 
     } 
    } 
} 
0

這裏是你的代碼,但是這是你的問題纔剛剛開始:

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(); 
      } 
     } 
    } 

我認爲這是嚴重的設想。使用UI/Swing類混合這樣的功能沒有什麼好的理由。將兩者分開,否則如果沒有Swing UI,您將永遠無法使用此方法。這就是所謂的分層或關注點分離。

0

更好的格式:

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; 
} 

保持代碼塊4個字符的縮進,即使你有與空間單獨做到這一點,刪除鍵(VS具有它會爲你的編輯器)。從長遠來看,它可以節省您的時間。

相關問題