javaUser javaUser - 3 months ago 25
Java Question

remove blank lines using java code

Below is my Java code which is used to create a

PDF
and print the content in the
PDF
document. I have blank lines between the printed lines as shown in the code below. If there are more than two blank lines after the printed statement, just write one blank line and omit other blank lines to write in the
PDF
document. So if there is one or two blank lines I can print it as it is, but if there are more than that then remove all extra blank lines and just add one blank line using Java code. I am using Java version 5. Please suggest.

public static void main(String args[]) {
Document document = null;
FileOutputStream fos = null;

try {
final String prefix = "test";
final File temporaryPDF = File.createTempFile(prefix, ".pdf");
document = new Document(PageSize.LETTER);

fos = new FileOutputStream(temporaryPDF);

PdfWriter.getInstance(document, fos);
document.open();

Font font = new Font(Font.FontFamily.COURIER, 10);

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.");

for (final String line : lines) {
document.add(new Paragraph(12, line, font));
}

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


PS: I again raised this post for more clarity. Thanks.

Answer

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.