0

Trying to remove any extra empty lines beyond 2 empty lines in a file made from a StringBuilder. The code is in groovy so I seem to not understand why simple java procedure don't seem to work. Tried 3 things... Any suggestions? See code snippets below

Regex doesn't do anything.

private static final removeExtraLines1(StringBuilder text)
{
    String twoLines = "MIAW";
    Pattern extraLines = Pattern.compile("\n")
    text.replaceAll(extraLines, twoLines);
}

My own method that deleteCharAt doesn't work too?

private void removeExtraLines(StringBuilder text)
{
    int i = 0;
    while(i < text.length()-1)
    {
        if(text.charAt(i) == System.lineSeparator() && text.charAt(i++) == System.lineSeparator())
        {
            text.deleteCharAt(i++);
            System.out.println("REPLACED AN EMPTY LINE");
        }
    }
}

Modifying the toString() method says regex method replaceAll is deprecated.

@Override 
String toString()
{
    def padding = " " * (4 * (level - 1) )
    padding + "cat level $level [$title]: " + 
        contained.collect { 
            it.toPaddedString(padding) }

    ////My Code
    String twoLines = System.lineSeparator()+System.lineSeparator();
    Pattern extraLines = Pattern.compile(System.lineSeparator()+ System.lineSeparator()+ System.lineSeparator()+System.lineSeparator()) 
    _toString().replaceAll(extraLines, twoLines)
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
kloklo90
  • 81
  • 1
  • 14

2 Answers2

3

The issue here is that each approach has a mistake:

  1. replaceAll() does not modify the StringBuilder. Instead, it returns a new String. Also, the regular expression is incorrect.
  2. Improper use of the post/pre-increment, as stated by Vijay.
  3. OK, I have no idea. I gave up trying to understand the third approach.

The easiest method is to use replaceAll() with a proper regular expression, and then use replace() to take the output of replaceAll() and make it the entire content of the StringBuilder:

private static final removeExtraLines1(StringBuilder text) {
    text.replace(0, text.size(), builder.replaceAll('\n\n', '\n'))
}
Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
  • Seems to do something now. The only issue is that my new lines aren't \n depending on the system I am using. Throughout the code we use System.lineSeparator(). So when I used replaceAll with '\n' it doesn't recognize empty line. Quickly removing that for another character present in the file proves that this approach does work. Can I use replaceAll with System.lineSeparator()? – kloklo90 May 13 '16 at 20:40
  • Oh, yeah I forgot about that. Yes, you can use `System.lineSeparator()`. – Emmanuel Rosa May 13 '16 at 20:42
1

you need to use ++i (pre increment) instead of i++ (post increment)

       if(text.charAt(i) == System.lineSeparator() && text.charAt(++i) == System.lineSeparator())
        {
            text.deleteCharAt(++i);
            System.out.println("REPLACED AN EMPTY LINE");
        }

checkout Pre & post increment operator behavior in C, C++, Java, & C#

Community
  • 1
  • 1
Vijay
  • 542
  • 4
  • 15