如何通过 BufferedReader 读取多行输入?

huangapple go评论81阅读模式
英文:

How do i read multiple lines of input via the BufferedReader?

问题

我正在开发一个从控制台接受输入的程序。目前为止,读取由控制台输入的单行输入没有问题。但是当程序应该读取多行输入时却无法正常工作。我该如何改进readInput方法以读取多行输入,并返回包含所有来自不同行的输入的单个字符串。

private String readInput() throws IOException {
    BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in));
    StringBuilder inputLines = new StringBuilder();
    String line;

    while ((line = inputStream.readLine()) != null && !line.isEmpty()) {
        inputLines.append(line).append("\n");
    }

    return inputLines.toString();
}
英文:

I am developing a program that takes input from the console. So far it has been no problem reading input that consists of one line of input from the console. But the program does not work when it is supposed to read multiple lines. How can i improve the readInput method to read multiple lines of input and the return a single String containing all of the input from different lines.

private String readIntput() throws IOException {
    BufferedReader inputstream = new BufferedReader(new InputStreamReader(System.in));
    String input = inputstream.readLine();

    return input;
}

答案1

得分: 1

当你写下String input = inputstream.readLine()时,这会一次读取一行,
由于你是从用户那里获取输入,即使用户按下回车键,也不会出现空的情况,你需要检查输入字符串的长度,如果是0,那么就从while循环中跳出。

但是当你从文件或其他来源读取时,情况就不同了,你需要检查输入是否为null

希望这对你有所帮助。

private String readInput() throws IOException {
    BufferedReader inputstream = new BufferedReader(new InputStreamReader(System.in));
    StringBuilder finalString = new StringBuilder();
    String input = inputstream.readLine();
    while (true) {
        finalString.append(input);
        input = inputstream.readLine();
        if (input.length() == 0) {
            break;
        }
    }
    inputstream.close();
    return finalString.toString();
}

输入:

hi hello
how are you
Am fine

输出:

hi hellohow are youAm fine
英文:

So when you write String input = inputstream.readLine() This reads one line at a time,
As you are taking input from the user there would not be any null cases even if the user clicks enter, You need to check for the length of the input string, If it is 0 then break from the while loop.

But this isn't the case when you are reading from a file or other source you need to check whether the input is null or not.

Hope this could help you.

private String readIntput() throws IOException {
    BufferedReader inputstream = new BufferedReader(new InputStreamReader(System.in));
    StringBuilder finalString = new StringBuilder();
    String input = inputstream.readLine();
    while(true){
       finalString.append(input);
       input=inputstream.readLine();
       if(input.length() == 0){
           break;
       }
    }
    br.close();
    return finalString;
}

Input:

hi hello
how are you
Am fine

Output:

hi hellohow are youAm fine

huangapple
  • 本文由 发表于 2020年9月24日 18:02:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64044112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定