BufferReader reading empty lines

核能气质少年 提交于 2019-12-10 23:05:41

问题


I am entering lines from input (IDE eclipse Luna) on pressing enter the cursor keeps moving down but no output shows up. In tried second method but i have to press enter twice to print output how can i fix both errors . Why in second method as soon as it detects blank line it doesn't print why i have to press enter twice And what commands goes when we press enter in console

FIRST METHOD

private static String para = null;
public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(System.in));
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        para = para + line;
    }
    bufferedReader.close();
    System.out.println(para);
}

SECOND METHOD

String line;
    while (!(line = br.readLine()).equals("")) {
        s1 = line.split(" ");
        for (int j = 1; j < s1.length; j++) {
            int m = Integer.parseInt(s1[j]);
            edges[Integer.parseInt(s1[0]) - 1].add(vertices[m - 1]);
        }

    }

回答1:


Method1 is not going to end ever as below readLine() will never returns null, it will always return empty String while reading from console input.

while ((line = bufferedReader.readLine()) != null) { para = para + line; }

Remember on pressing enter java considers it as newLine() command, which surely not null, so your program goes on with empty string and never terminates from the while loop.

I would suggest to have some meaning full check to terminate while loop. You can try Scanner instead of BufferedReader as it provides lot of utility methods.

In Scanner you have methods like hasNext(), hasNextLine() etc which tells whether you have further input or not. Similarly methods like nextLine(), nextInt(), nextDouble() are there to get specific converted values from the command line or any other stream.

Method2 is doing lot of operations which were not part of the Method1, can you please share full runnable code for the same. I doubt on the logic again for terminating the loop.

So I would suggest to use more specific check to terminate your loop while iterating through some data.




回答2:


When you press the entre key the bufferedReader object receive an empty string so it's not null.

To end the input should look for a key such as this :

private static String para = null;

public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(System.in));
    String line;
    while (!"end".equals((line = bufferedReader.readLine()))) {
        para = para + line;
    }
    bufferedReader.close();
    System.out.println(para);
}

here I choose 'end' as a ending sequence but you can choose anything such as empty string wich is the result for pressing enter on an empty line.




回答3:


no output shows up why??

because you are not printing any thing inside loop

while (!(line = bufferedReader.readLine()).equals("")) // read until enter without inputting anything is pressed.

private static String para = ""; // initialize your para with empty string(i.e "") not with null otherwise null will be added to your para string.
public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(System.in));
    String line=bufferedReader.readLine();  
    do{
         System.out.println(line);  // print what you input
         para = para + line;
    }
    while (!(line = bufferedReader.readLine()).equals("")); 
   bufferedReader.close();
    System.out.println(para);
}


来源:https://stackoverflow.com/questions/32606181/bufferreader-reading-empty-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!