When handling user input through Scanner(System.in), you can use System.setIn() to automate user input.

Like this:

package edu.ntnu.idi.idat; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Scanner; public final class TestCode { private TestCode() { } /** * Test code. * @param args */ public static void main( final String[] args ) { InputStream input = System.in; String testInputString = "test" + System.lineSeparator() + "test2"; ByteArrayInputStream testInput = new ByteArrayInputStream(testInputString.getBytes()); System.setIn(testInput); Scanner inputScanner = new Scanner(System.in); System.out.println(inputScanner.nextLine()); System.out.println(inputScanner.nextLine()); inputScanner.close() } }

But when trying the same method for JLine's LineReader, it doesn't work:

package edu.ntnu.idi.idat; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; import org.jline.terminal.Terminal; import org.jline.terminal.TerminalBuilder; public final class TestCode { private TestCode() { } /** * Test code. * @param args * @throws IOException */ public static void main( final String[] args ) throws IOException { InputStream input = System.in; String testInputString = "test" + System.lineSeparator() + "test2"; ByteArrayInputStream testInput = new ByteArrayInputStream(testInputString.getBytes()); System.setIn(testInput); Terminal terminal = TerminalBuilder.builder().build(); LineReader lineReader = LineReaderBuilder .builder() .terminal(terminal) .build(); System.out.println(lineReader.readLine()); System.out.println(lineReader.readLine()); terminal.close() } }

I believe this is because inputScanner uses System.in (as can be seen when constructing it), while LineReader doesn't seem to.

My question is: is there a way to automate user input for LineReader as well?

Lucy's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.