[Solved] remove blank lines using java code [closed]


Here is a minimal answer that should resolve your problem. I did reduce the code to the necessary parts since I needed to test it myself and I didn’t have access to classes you used.

You should make the code in the question as minimal as possible when asking a question, so it is easier to recreate.

public static void main(String args[]) {
    List<String> lines = new ArrayList<String>();
    lines.add("This is first line.");
    lines.add("This is second line.");
    lines.add(" ");
    lines.add(" ");
    lines.add(" ");
    lines.add(" ");
    lines.add("This is third printed line.");
    lines.add(" ");
    lines.add("This is fourth printed line.");
    lines.add(" ");
    lines.add(" ");
    lines.add(" ");
    lines.add("#ACC004342-123");
    lines.add(" ");
    lines.add(" ");
    lines.add(" ");
    lines.add(" ");
    lines.add(" ");
    lines.add(" ");
    lines.add("More information:");
    lines.add("This is fifth printed line.");
    lines.add("#ACC004342-123");
    lines.add("");
    lines.add("This is Sixth printed line.");
    lines.add("Some information goes here.");

    //count empty lines
    int emptyLineCounter = 0; 

    for (final String line : lines) {
        //when empty line increment counter, else reset counter
        if(line.trim().length() == 0) 
            emptyLineCounter++;
        else
            emptyLineCounter = 0;

        //when more then 2 empty lines are encountered move ignore line
        if(emptyLineCounter <= 2)
            System.out.println(line); //add line to document
    }
}

Edit: Others having written wrong answers is a direct result of not providing minimal, complete and verifiable code.

solved remove blank lines using java code [closed]