【问题标题】:Java console program not letting me input stringJava控制台程序不允许我输入字符串
【发布时间】:2013-01-25 19:15:33
【问题描述】:

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

我的代码允许用户在第一部分说yn,但在我的条件语句中它不允许用户输入列表的名称;它只是完成了程序。没有错误信息;它只是没有按我的预期运行。这是我的代码:

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")
    }

提前感谢您在这方面的时间和帮助!

【问题讨论】:

  • 我开始尝试进行一些调试,但对于我的可变“字符串列表名称”,它不能被识别为变量;因此没有任何东西被传递给它。当我将鼠标悬停在 listName 上时,会说“listName =>”listName”在当前上下文中不是已知变量。

标签: java console-application bufferedreader conditional-statements


【解决方案1】:

改变

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

这是您期望的输出吗?

【讨论】:

  • 是的,这正是我想要做的!谢谢你:)
  • 我必须等待 5 分钟才能将其标记为答案。不过有一件事,user000001,你错过了 br.readLine() 之后的分号,如果有办法我可以为你添加它或者如果必须编辑它。
  • 哦好的不知道这个限制...修复了这个错误
【解决方案2】:
...
String answer = br.readLine ();
if (answer.startsWith ("y")) {
...

【讨论】:

    【解决方案3】:

    问题是 read()。它不会读取由于 Enter 而出现的 newLine。

     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,感谢您对其工作原理的补充见解,从技术上讲,感谢您的宝贵时间!
    猜你喜欢
    • 1970-01-01
    • 2014-02-02
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2018-08-31
    • 1970-01-01
    相关资源
    最近更新 更多