Search code examples
javabufferedreader

Bufferedreader next line after read() method


Let's say I need to input character and afterwards a word. Here is my piece of code, but I don't think it'll help in this particular case.

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
char letter = (char)reader.read();
String input = reader.readLine();

So if I enter d and press Enter, I will terminate the program. The only possible solution is to enter character, press Space and enter the word.

However, I'm eager to know how to enter the word from the next line. If I switch String with char it will work (assumebly because readLine() moves to the new line).

Usually I would not write here to find out answers for some small questions, but this time my Google skills failed me. Will appreciate any guidance:)

enter image description here


Solution

  • If you instantiate buffered reader two times then you can get string in next line after entering character

        BufferedReader reader; 
    
        reader = new BufferedReader(new InputStreamReader(System.in));
        char letter = (char)reader.read();
    
        reader = new BufferedReader(new InputStreamReader(System.in));
        String input = reader.readLine();