简体   繁体   中英

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM