0

Please help me to input by line by line via java console. Now i can give input only as one line. How to give multiple inputs in line by line??

1
  • 2
    Use loop? Read multiple times? Commented Aug 13, 2012 at 13:58

4 Answers 4

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);
          }
      }
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use Scanner and loop through to ask for multiple times.

For example

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

Comments

0

I'm not sure I understand your question but...

 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);

Comments

0

Use the new Console class:

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

End the input by pressing ^Z (control-Z) followed by ENTER.

It has one caveat and that is that the console can be null inside an IDE. Try it from the command-line and you should be fine:

java path.to.my.MainClass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.