0

当我在 Windows 命令行中运行以下行时,它不采用第一个字母。如果我输入22它只打印'2'

private static String  readInput() {
    try {
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         input = br.readLine();
         System.out.println("input "+input);

   } catch (Exception ioe) {
      System.out.println("Error trying to read your input!");
      ioe.printStackTrace();
   }
}       
4

3 回答 3

0

Ok I found the problem. Its not relate to scanner or BufferedReader. All works fine. One of my another back end thread is reading System.in.read();. That's why I always missed first input. Sorry for confusing all of you.

于 2014-03-12T09:47:47.230 回答
0

试试这样!

private static String  readInput() {
try {
     DataInputStream in=new DataInputStream(System.in);
     input = in.readLine();
     System.out.println("input "+input);
     return input;
   } catch (Exception ioe) {
      System.out.println("Error trying to read your input!");
      ioe.printStackTrace();
   }
}     
于 2014-03-12T08:19:19.890 回答
0

尝试这个。

   while((input = br.readLine()) != null){
         System.out.println("input "+input);
    }
于 2014-03-12T08:06:37.363 回答