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??
4 Answers
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);
}
}
Comments
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