[英]Java: Skipping the first line of user input when appending a text file?
So I'm trying to accept lines of text from the user so that it can be appended to a txt file. 因此,我正在尝试接受用户的文本行,以便可以将其附加到txt文件中。 However, after the program runs, the first line the user entered does not appear in the appended file. 但是,程序运行后,用户输入的第一行不会出现在附加文件中。
Here is a portion of the code: 这是部分代码:
System.out.println("enter advice (hit return on empty line to quit):");
String advice = keyboard.nextLine();
FileWriter fw = new FileWriter(inputFile, true);
PrintWriter pw = new PrintWriter(fw);
for(int n = 1; n <= 2; ++n)
{
advice = keyboard.nextLine();
pw.print(advice);
}
pw.close();
keyboard.close();
Here is a sample run of the code: 这是代码的示例运行:
$ java Advice
enter input filename: Advice.txt **enter**
1: fully understand the given problem
2: do analysis and design first before typing
enter advice (hit return on empty line to quit):
a **enter**
b **enter**
Thank you, goodbye!
$ cat Advice.txt
1: fully understand the given problem
2: do analysis and design first before typing
b
Any thoughts? 有什么想法吗? Thanks! 谢谢!
The first line the user enters is being captured but not being written to the file. 用户输入的第一行已被捕获,但未写入文件中。 String advice = keyboard.nextLine();
When you write to the file you are taking in the next user input 写入文件时,您将接受下一个用户输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.