I am trying to pass a String to my BufferedReader
. How can I pass "test" as String
to the reader rather than the input from System.in
?
String test = "test"; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
This class provides a method known as readLine() which reads and returns the next line from the source and returns it in String format. The BufferedReader class doesn't provide any direct method to read an integer from the user you need to rely on the readLine() method to read integers too.
The BufferedReader can't read the InputStream directly; So, we need to use an adapter like InputStreamReader to convert bytes to characters format. For example: // BufferedReader -> InputStreamReader -> InputStream BufferedReader br = new BufferedReader( new InputStreamReader(inputStream, StandardCharsets. UTF_8));
You can modify your code as below
String test = "test"; Reader inputString = new StringReader(test); BufferedReader reader = new BufferedReader(inputString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With