Java控制台程序不允许我输入字符串

Java控制台程序不允许我输入字符串,java,console-application,bufferedreader,conditional-statements,Java,Console Application,Bufferedreader,Conditional Statements,我试图创建一个简单的控制台程序,询问用户是否要创建列表,如果“是”,允许他们输入列表的名称。然后,它应该在退出程序之前“打印”列表的名称 我的代码允许用户对第一部分说y或n,但在我的条件语句中,它不允许用户输入列表的名称;它只是完成了程序。没有错误信息;它只是没有像我预期的那样工作。这是我的密码: public static void main(String[] args) throws IOException { getAnswers(); } public static

我试图创建一个简单的控制台程序,询问用户是否要创建列表,如果“是”,允许他们输入列表的名称。然后,它应该在退出程序之前“打印”列表的名称

我的代码允许用户对第一部分说
y
n
,但在我的条件语句中,它不允许用户输入列表的名称;它只是完成了程序。没有错误信息;它只是没有像我预期的那样工作。这是我的密码:

public static void main(String[] args) throws IOException     
{
    getAnswers();
}

public static void getAnswers()throws IOException{
    char answer;
    String listName;        
    BufferedReader br = new BufferedReader 
            (new InputStreamReader(System.in));        
    System.out.println ("Would like to create a list (y/n)? ");

    answer = (char) br.read();        
    ***if (answer == 'y'){
        System.out.println("Enter the name of the list: ");
        listName = br.readLine();
        System.out.println ("The name of your list is: " + listName);}***
    //insert code to save name to innerList
    else if (answer == 'n'){ 
        System.out.println ("No list created, yet");
    }
    //check if lists exist in innerList
    // print existing classes; if no classes 
    // system.out.println ("No lists where created. Press any key to exit")
    }
提前感谢您的时间和帮助

变化

answer = (char) br.read();

为了在用户按下
y
n

完整代码:

import java.io.*;

class Test {

    public static void main(String[] args) throws IOException {
         getAnswers();
    }

    public static void getAnswers() throws IOException {
         char answer;
         String listName;        
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        
         System.out.println ("Would like to create a list (y/n)? ");
         answer = (char) br.read(); 
         br.readLine(); // <--    ADDED THIS 
         if (answer == 'y'){
             System.out.println("Enter the name of the list: ");
             listName = br.readLine();
             System.out.println ("The name of your list is: " + listName);
         }
         //insert code to save name to innerList
         else if (answer == 'n') { 
             System.out.println ("No list created, yet");
         }
         //check if lists exist in innerList
         // print existing classes; if no classes 
         // system.out.println ("No lists where created. Press any key to exit")
    }
}
这是您期望的输出吗?

问题是read()。它不会读取因Enter而出现的换行符

...
String answer = br.readLine ();
if (answer.startsWith ("y")) {
...
 answer = (char) br.read();        // so if you enter 'y'+enter then -> 'y\n' and read will read only 'y' and the \n is readed by the nextline.
    br.readLine();    // this line will consume \n to allow next readLine from accept input.

我开始尝试做一些调试,对于我的“stringlistname”,它不能被识别为变量;因此,没有任何东西被传递给它。当我将鼠标悬停在listName上时,显示“listName=>”在当前上下文中不是一个已知变量。是的,这正是我要做的!谢谢:)我不得不等待5分钟才能将其标记为答案。不过,有一件事,user000001,您缺少br.readLine()后面的分号,idk,如果有一种方法我可以为您添加它,或者如果必须编辑它。哦,好的,我不知道这个限制…修复了bugArpit,感谢您对它的工作原理有更多的见解,技术上,我感谢您的时间!
...
String answer = br.readLine ();
if (answer.startsWith ("y")) {
...
 answer = (char) br.read();        // so if you enter 'y'+enter then -> 'y\n' and read will read only 'y' and the \n is readed by the nextline.
    br.readLine();    // this line will consume \n to allow next readLine from accept input.