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

3
我正在尝试创建一个简单的控制台程序,询问用户是否要创建列表,如果是,则允许他们输入列表名称。然后在退出程序之前,应该“打印”列表名称。
我的代码允许用户在第一部分中回答“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")
    }

非常感谢您在此事上的时间和帮助!


我开始尝试进行一些调试,但我的变量"String listName"没有被识别为变量;因此没有任何内容被传递给它。当我将鼠标悬停在listName上时,它显示"listName => "listName"不是当前上下文中已知的变量。 - Chris
3个回答

4

更改

answer = (char) br.read();

为了

answer = (char) br.read();br.readLine();

为了在用户按下 yn 后读取换行符。
完整代码:
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")
    }
}

输出:
Would like to create a list (y/n)? 
y
Enter the name of the list: 
Mylist
The name of your list is: Mylist

你期望的输出是这个吗?

是的,那正是我想要做的!谢谢 :) - Chris
我必须等待5分钟才能将其标记为答案。不过,user000001,你在br.readLine()后面漏了分号,我不知道是否有办法为你添加它或者你需要自己编辑。 - Chris
哦,好的,我不知道那个限制...已经修复了这个错误。 - user000001

2
问题在于read()方法。它不会读取由"Enter"键产生的新行符号。
 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.

Arpit,感谢您对技术方面的深入解释,我很感激您的时间! - Chris

0
...
String answer = br.readLine ();
if (answer.startsWith ("y")) {
...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接