我正在尝试在Java程序中阅读标准输入。我期望一系列数字后跟换行符,例如:
6 9 1
当通过eclipse内置控制台提供输入时,一切都会顺利进行。但是,使用Windows命令行时,程序将输出:
Received '6'. Received 'null'. Invalid input. Terminating. (This line is written by another function that does an Integer.parseint()).
我的代码是:
static String readLineFromStdIn(){ try{ java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String input = new String(); input = stdin.readLine(); System.out.println("Received '" + input + "'"); return(input); }catch (java.io.IOException e) { System.out.println(e); } return "This should not have happened"; }
有什么线索吗?
得到a null表示相关Reader对象已到达EOF(文件末尾),或者换句话说,它们无法再获得任何标准输入。现在,您的代码明显的问题是:
null
Reader
readLineFromStdIn()
BufferedReader
System.in
解决方案是对的每次调用使用 单个 共享库。BufferedReader``readLineFromStdIn()
BufferedReader``readLineFromStdIn()