我要做的是从最终用户那里获取输入并将其写入名为 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.*;
如果有人可以告诉我为什么我会收到此错误并告诉我如何修复它,我将非常感激。
答案 0 :(得分: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();
}
}
}
答案 1 :(得分:1)
在此{}
块
else{
FileWriter aDictionary = new FileWriter("dictionary.txt", true);
PrintWriter out = new PrintWriter(aDictionary);
out.println(newWord);
out.close();
aDictionary.close();
}
答案 2 :(得分: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();
}
}
}
答案 3 :(得分: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的情况下使用此方法。它被称为分层或关注点分离。
答案 4 :(得分: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个字符的缩进,即使你必须单独使用空格和删除键(与编辑器一起为你完成)。从长远来看,它可以节省您的时间。