为什么循环中的第一个 .nextline 会被跳过,然后下次不会被跳过?以及如何阻止它被覆盖?

发布于 2024-12-29 02:16:36 字数 1889 浏览 3 评论 0原文

我需要创建一个基本程序来将文本输入到 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛上了 2025-01-05 02:16:36

当您为 nextInt() 输入 int 时,您还按下 Enter 来接收输入,这会转换为同时读取的新行。该新行被视为下次调用 nextLine() 的输入。您需要在调用 nextInt() 后添加一个人工 nextLine() 或直接使用 nextLine() 并将输入解析为int

count = sc.nextInt();
sc.nextLine();

count = Integer.parseInt(sc.nextLine());

When you are inputting the int for nextInt() 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 to nextLine(). You will need to put an artificial nextLine() after the call to nextInt() or use nextLine() directly and parse the input as an int:

count = sc.nextInt();
sc.nextLine();

or

count = Integer.parseInt(sc.nextLine());
围归者 2025-01-05 02:16:36

.nextInt() 不会捕获您的 Enter 键。您需要在其后面放置一个空白的 .nextLine() 。

.nextInt() does not catch your Enter press. You'll need to put a blank .nextLine() after it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文