【问题标题】:Java BufferedReader readLine() suddenly not working after read()Java BufferedReader readLine() 在 read() 后突然不起作用
【发布时间】:2020-02-10 16:05:12
【问题描述】:

我目前的循环有问题。我输入一次字符串后,它会提示用户,当满足循环条件时,它只是不断询问用户“你想继续吗?”并且无法输入另一个字符串。

public static void main(String[] args) throws IOException
{
    BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
    LinkedList<String> strList = new LinkedList();
    char choice;

    do
    {
        System.out.print("Add Content: ");
        strList.add(bfr.readLine());
        System.out.print("Do you want to add again? Y/N?");
        choice = (char)bfr.read();

    }
    while(choice == 'Y');

}

【问题讨论】:

  • 当你输入Y时,你也在它后面输入一个换行符。因为您只读取一个字符,所以您不读取换行符。只需使用readLine 来阅读您的选择。
  • 您可以从与Scanner 相关的question 中阅读一些很好的解释,但问题是由同一件事引起的。

标签: java bufferedreader readline


【解决方案1】:

您需要从键盘缓冲区中取出换行符。你可以这样做:

do
{
    System.out.print("Add Content: ");
    strList.add(bfr.readLine());
    System.out.print("Do you want to add again? Y/N?");
    //choice = (char)bfr.read();

    choice = bfr.readLine().charAt(0); // you might want to check that a character actually has been entered. If no Y or N has been entered, you will get an IndexOutOfBoundsException
 }
 while(choice == 'Y');

【讨论】:

    【解决方案2】:

    通常终端只会在您按下回车后发送数据。因此,当您再次执行 readLine 时,您会得到一个空行。您必须阅读一行,然后检查它是否包含Y。或者之后再阅读空行,以您认为不太容易出错的方式为准。

    我倾向于使用较早的内容并阅读整行,然后检查其中包含的内容。

    【讨论】:

      【解决方案3】:

      不要忘记,对于像这个程序这样简单的东西,您可以使用方便的java.io.Console 类。

              Console console = System.console();
              LinkedList<String> list = new LinkedList<>();
              char choice = 'N';
      
              do {
                  System.out.print("Add Content: ");
                  list.add(console.readLine());
                  System.out.print("Do you want to add again? Y/N?\n");
                  choice = console.readLine().charAt(0);
      
              } while (choice == 'Y' || choice == 'y');
      

      【讨论】:

        【解决方案4】:

        这项工作(不要忘记图书馆):

        public static void main(String[] args) throws IOException
        {
            BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
            List<String> strList = new ArrayList<String>();
            String choice;
        
            do {
                System.out.println("Add Content: ");    
                strList.add(bfr.readLine());
                System.out.println("Do you want to add again? Y/N?");
                choice = bfr.readLine();
        
            } while(choice.equals("Y"));
        
            System.out.println("End.");
        
        }
        

        【讨论】:

          【解决方案5】:

          试试这个:

              public static void main(String[] args) throws IOException
                  {
                  Scanner scan=new Scanner(System.in);
                  LinkedList<String> strList = new LinkedList();
                  String choice;`
          
                  do
                  {
                      System.out.print("Add Content: ");
                      strList.add(scan.nextLine());
                      System.out.print("Do you want to add again? Y/N?");
                      choice = scan.nextLine();
          
                  }
                  while(choice.equals("Y"));
          
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-05-10
            • 2015-06-07
            • 2021-09-30
            • 2018-05-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多