Thuan Nguyen
Thuan Nguyen

Reputation: 501

java - BufferedReader readLine stop reading when encouters empty string

I am using BufferedReader to read a text file line by line. Then i use a method to normalize each line text. But there is something wrong with my normalization method, after the call to it, BufferedReader object stop reading file. Can someone help me with this.

Here is my code:

public static void main(String[] args) {
    String string = "";

    try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
        String line;
        while ((line = br.readLine()) != null) {

            string += normalize(line);

        }
    } catch (Exception e) {

    }
    System.out.println(string);
}

public static String normalize(String string) {

    StringBuilder text = new StringBuilder(string.trim());



    for(int i = 0; i < text.length(); i++) {
        if(text.charAt(i) == ' ') {
            removeWhiteSpaces(i + 1, text);
        }
    }

    if(text.charAt(text.length() - 1) != '.') {
        text.append('.');
    }

    text.append("\n");
    return text.toString();
}

public static void removeWhiteSpaces(int index, StringBuilder text) {
        int j = index;
        while(text.charAt(j) == ' ') {
            text.deleteCharAt(j);
        }
    }

and here is the text file that i use:

abc .

 asd.



 dasd.

Upvotes: 0

Views: 2463

Answers (6)

Victor Calatramas
Victor Calatramas

Reputation: 869

The problem is not in your code but in the understanding of the readLine() method. In the documentation is stated:

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()

So that means that if the method finds an empty line it will stop reading and return null.

The code proposed by @tijn167 would do the workaround using BufferedReader. If you are not restraint to BufferedReader use ScanReader as @Abhishek Soni suggested.

Also, your method removeWhiteSpaces() is checking for white spaces while the empty lines are not a white space but a carry return \r or a line feed \n or both. So your condition text.charAt(j) == ' ' is never satisfied.

Upvotes: 1

rockfarkas
rockfarkas

Reputation: 132

I think you have problem in your removeWhiteSpaces(i + 1, text);, and if you have problem in the string process, the reader wont able to read the next line.

You don't check the empty string, and you call text.charAt(text.length()-1), it is a problem too.

Print the exception, change your catch block to write out the exception:

} catch (Exception e) {
    e.printStackTrace();
}

The reason is in your while(text.charAt(j) == ' ') {, you don't examine the length of StringBuilder, but you delete it...

Upvotes: 3

Alan
Alan

Reputation: 976

The normalize function is causing this. the following tweak to it shoudl fix this:

public static String normalize(String string) {

        if(string.length() < 1) {
            return "";
        }
        StringBuilder text = new StringBuilder(string.trim());
        if(text.length() < 1){
            return "";
        }


        for(int i = 0; i < text.length(); i++) {
            if(text.charAt(i) == ' ') {
                removeWhiteSpaces(i + 1, text);
            }
        }

        if(text.charAt(text.length() - 1) != '.') {
            text.append('.');
        }

        text.append("\n");
        return text.toString();
    }

Upvotes: 1

anzeha
anzeha

Reputation: 1

Second line of your file is empty, therefore the while loop stops

Upvotes: -1

Abhishek Soni
Abhishek Soni

Reputation: 593

Try ScanReader

 Scanner scan = new Scanner(is);
 int rowCount = 0;
 while (scan.hasNextLine()) {
             String temp = scan.nextLine();

             if(temp.trim().length()==0){
                 continue;
             }
}

//rest of your logic

Upvotes: 2

tijn167
tijn167

Reputation: 597

Try this:

   while ((line = br.readLine()) != null) {
        if(line.trim().isEmpty()) {
            continue;
        }
         string += normalize(line);
      }

Upvotes: 1

Related Questions