通过java控制台逐行输入

时间:2012-08-13 13:56:55

标签: java command-line input console

请帮我通过java控制台逐行输入。现在我只能输入一行。如何逐行提供多个输入?

4 个答案:

答案 0 :(得分:1)

      InputStreamReader converter = new InputStreamReader(System.in);
      BufferedReader in = new BufferedReader(converter);

      String CurLine = ""; // Line read from standard in

      while (!(CurLine.equals("quit"))){
          CurLine = in.readLine();

          if (!(CurLine.equals("quit"))){
              System.out.println("You typed: " + CurLine);
          }
      }

答案 1 :(得分:0)

您需要使用扫描仪并循环询问多次。

例如

Scanner stdin = new Scanner(new BufferedInputStream(System.in));
while (stdin.hasNext()) {
//Get input and do your logic.
}

答案 2 :(得分:0)

我不确定我理解你的问题但是......

 final List<String> inputs = new ArrayList<String>();
 final Scanner in = new Scanner(System.in);
 for (int i = 0; i < 5; i++) {
      System.out.print("> ");
      inputs.add(in.next());
 }
 System.out.println(inputs);

答案 3 :(得分:0)

使用 new 控制台类:

Console console = System.console();
if (console != null) {
    Scanner scanner = new Scanner(console.reader());
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        // Do something with your line
    }
}

^Z(control-Z),然后按ENTER结束输入。

有一点需要注意,即console在IDE中可以为null。从命令行尝试它你应该没问题:

java path.to.my.MainClass