[英]Java BufferedReader.readLine() not waiting for user input
I am having an issue with my instance of BufferedReader not waiting for the user to input before moving forward.我的 BufferedReader 实例出现问题,在继续之前没有等待用户输入。
static BufferedReader in;
public class {
public static void main(String[] args) {
try{
in = new BufferedReader(new InputStreamReader(System.in));
String input;
System.out.println("prompt");
input = in.readLine();
if(input.equals("Y")){
//do something
}else {
//do something else
}
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
What happens is I when I run the program, it simply skips over the in.readLine() and throws a null exception at the if statement.当我运行程序时会发生什么,它只是跳过 in.readLine() 并在 if 语句中抛出一个空异常。 I am at a loss on what is happening, as I used the same code for another project that still works.
我对正在发生的事情感到茫然,因为我在另一个仍然有效的项目中使用了相同的代码。
Thanks!谢谢!
Found the answer.找到了答案。 Using gradle to run the program and my task didn't have the parameter for standardInput = System.in.
使用gradle运行程序,我的任务没有standardInput = System.in的参数。 Still getting used to project building software!
仍然习惯于项目构建软件!
Yes you have to treat the exception, just add this:是的,您必须处理异常,只需添加以下内容:
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
String input;
System.out.println("prompt");
input = in.readLine();
if(input.equals("Y")){
System.out.println("it works !!");
}else {
System.out.println("it's not a Y :(");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.