为什么循环中的第一个 .nextline 会被跳过,然后下次不会被跳过?以及如何阻止它被覆盖?
我需要创建一个基本程序来将文本输入到 .txt 文档,因此我创建了这个程序,但我不明白为什么在程序首次运行时会跳过第一个问题。
如果设置循环的第一个问题不存在,则不会发生这种情况。另外,当我只想添加内容时,如何阻止它覆盖 txt 文档中已有的内容。
到目前为止,最后一种方法似乎很有效,但我想我仍然会包括它。
package productfile;
import java.io.*;
import java.util.Scanner;
/**
*
* @author Mp
*/
public class Products {
public void inputDetails(){
int i=0;
int count=0;
String name;
String description;
String price;
Scanner sc = new Scanner(System.in);
System.out.println("How many products would you like to enter?");
count = sc.nextInt();
do{
try{
FileWriter fw = new FileWriter("c:/Users/Mp/test.txt");
PrintWriter pw = new PrintWriter (fw);
System.out.println("Please enter the product name.");
name = sc.nextLine();
pw.println("Product name: " + name );
System.out.println("Please enter the product description.");
description = sc.nextLine();
pw.println("Product description: " + description );
System.out.println("Please enter the product price.");
price = sc.nextLine();
pw.println("Product price: " + price );
pw.flush();
pw.close();
i++;
}catch (IOException e){
System.err.println("We have had an input/output error:");
System.err.println(e.getMessage());
}
} while (i<count);
}
public void display(){
String textLine;
try{
FileReader fr = new FileReader("c:/Users/Mp/test.txt");
BufferedReader br = new BufferedReader(fr);
do{
textLine = br.readLine();
if (textLine == null){
return;
} else {
System.out.println(textLine);
}
} while (textLine != null);
}catch(IOException e){
System.err.println("We have had an input/output error:");
System.err.println(e.getMessage());
}
}
}
I needed to create a basic program to input text to a .txt document so I created this but I don't see why the first question is skipped when the program is first run.
It doesn't happen if the first question that sets the loop isn't there. Also how do I stop it from overwriting what is already there in the txt document when all I want it to do is add to it.
So far the last method seems to work far but I thought I would still include it.
package productfile;
import java.io.*;
import java.util.Scanner;
/**
*
* @author Mp
*/
public class Products {
public void inputDetails(){
int i=0;
int count=0;
String name;
String description;
String price;
Scanner sc = new Scanner(System.in);
System.out.println("How many products would you like to enter?");
count = sc.nextInt();
do{
try{
FileWriter fw = new FileWriter("c:/Users/Mp/test.txt");
PrintWriter pw = new PrintWriter (fw);
System.out.println("Please enter the product name.");
name = sc.nextLine();
pw.println("Product name: " + name );
System.out.println("Please enter the product description.");
description = sc.nextLine();
pw.println("Product description: " + description );
System.out.println("Please enter the product price.");
price = sc.nextLine();
pw.println("Product price: " + price );
pw.flush();
pw.close();
i++;
}catch (IOException e){
System.err.println("We have had an input/output error:");
System.err.println(e.getMessage());
}
} while (i<count);
}
public void display(){
String textLine;
try{
FileReader fr = new FileReader("c:/Users/Mp/test.txt");
BufferedReader br = new BufferedReader(fr);
do{
textLine = br.readLine();
if (textLine == null){
return;
} else {
System.out.println(textLine);
}
} while (textLine != null);
}catch(IOException e){
System.err.println("We have had an input/output error:");
System.err.println(e.getMessage());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您为
nextInt()
输入int
时,您还按下 Enter 来接收输入,这会转换为同时读取的新行。该新行被视为下次调用nextLine()
的输入。您需要在调用nextInt()
后添加一个人工nextLine()
或直接使用nextLine()
并将输入解析为int
:或
When you are inputting the
int
fornextInt()
you are also pressing enter for the input to be received, which translates into a new line being also read. This new line is considered input for your next call tonextLine()
. You will need to put an artificialnextLine()
after the call tonextInt()
or usenextLine()
directly and parse the input as anint
:or
.nextInt() 不会捕获您的 Enter 键。您需要在其后面放置一个空白的 .nextLine() 。
.nextInt() does not catch your Enter press. You'll need to put a blank .nextLine() after it.