Java 选中此项可在存储用户输入之前退出程序
我有一个几乎完成的程序。唯一的问题是,当用户输入“exit”以终止程序时,“exit”一词会写入最后的文件“quotes.txt”。如何让程序先检查“退出”,而不将其写入“quotes.txt”Java 选中此项可在存储用户输入之前退出程序,java,do-while,bufferedwriter,Java,Do While,Bufferedwriter,我有一个几乎完成的程序。唯一的问题是,当用户输入“exit”以终止程序时,“exit”一词会写入最后的文件“quotes.txt”。如何让程序先检查“退出”,而不将其写入“quotes.txt” System.out.println("Enter your text(Type 'exit' to close program.): "); bw = new BufferedWriter(new FileWriter(quotesFile, true)
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
input = kb.nextLine();
if(!input.equalsIgnoreCase("exit")) {
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
}
}
代码如下:
public static void main(String[] args) throws IOException {
final Formatter fo;
BufferedWriter bw = null;
BufferedReader in = new BufferedReader(new FileReader("quotes.txt"));
String input = "";
String line;
File quotesFile = new File("quotes.txt");
if (quotesFile.exists()) {
System.out.println(quotesFile.getName() + " exists.");
} else {
System.out.println("THIS DOES NOT EXIST.");
}
try {
fo = new Formatter("quotes.txt");
System.out.println("File created or found.");
} catch (Exception e) {
System.out.println("You have an error.");
}
do {
try {
Scanner kb = new Scanner(System.in);
if (!input.equalsIgnoreCase("exit")) {
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
input = kb.nextLine();
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
}
} catch (Exception e) {
System.out.println("Error.");
}
} while (!input.equalsIgnoreCase("exit"));
System.out.println("Results: ");
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
}
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
input = kb.nextLine();
if(!input.equalsIgnoreCase("exit")) {
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
}
}
在将输入写入文件之前,请检查输入内容
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
input = kb.nextLine();
if(!input.equalsIgnoreCase("exit")) {
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
}
}
在将输入写入文件之前,请检查输入内容
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
input = kb.nextLine();
if(!input.equalsIgnoreCase("exit")) {
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
}
}
您只能实例化扫描仪和写入程序一次。问题的关键在于测试后检查输入。请注意,您复制了测试:一个在
if
中,另一个在while
中。我建议您使用以下算法:
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
input = kb.nextLine();
if(!input.equalsIgnoreCase("exit")) {
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
}
}
Scanner kb = new Scanner(System.in);
input = kb.nextLine();
while (!input.equalsIgnoreCase("exit")) {
try {
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
} catch (Exception e) {
System.out.println("Error.");
}
input = kb.nextLine();
}
请注意,
do…而
并不能最好地满足您的需求。您只能将扫描仪和书写器实例化一次。问题的关键在于测试后检查输入。请注意,您复制了测试:一个在if
中,另一个在while
中。我建议您使用以下算法:
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
input = kb.nextLine();
if(!input.equalsIgnoreCase("exit")) {
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
}
}
Scanner kb = new Scanner(System.in);
input = kb.nextLine();
while (!input.equalsIgnoreCase("exit")) {
try {
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
} catch (Exception e) {
System.out.println("Error.");
}
input = kb.nextLine();
}
请注意,
do…while
不会对您的需求做出最佳响应。不要执行while循环,而是执行while循环。此外,您不需要每次在循环中都创建一个新的BufferedWriter。此外,输入可能包含空格和/或换行符,因此您应该尝试比较input.trim()。而不是执行while循环,而是执行while循环。此外,您不需要每次在循环中都创建一个新的BufferedWriter。此外,输入可能包含空格和/或换行符,因此您应该尝试比较input.trim()。感谢您提供了另一种方法,而不是do while。我听说如果你能避免的话,使用do while不是最好的做法。不客气!注意你的脚本,我看到你接受了前面的答案,但检查你如何打开/关闭你的作者:当你打开一个时,应该(必须)关闭它。一个很好的做法是将bw.close()放在您的案例中在捕获后的最后
块中的code>。此外,您可以在循环外打开Writer,然后在循环结束后关闭它。祝你好运感谢您提供了另一种方式,而不是边做边做。我听说如果你能避免的话,使用do while不是最好的做法。不客气!注意你的脚本,我看到你接受了前面的答案,但检查你如何打开/关闭你的作者:当你打开一个时,应该(必须)关闭它。一个很好的做法是将bw.close()放在您的案例中在捕获后的最后
块中的code>。此外,您可以在循环外打开Writer,然后在循环结束后关闭它。祝你好运
System.out.println("Enter your text(Type 'exit' to close program.): ");
bw = new BufferedWriter(new FileWriter(quotesFile, true));
input = kb.nextLine();
if(!input.equalsIgnoreCase("exit")) {
bw.write(input);
bw.newLine();
bw.close();
System.out.println("Entry added.\n");
}
}