java 删除空行_使用java代码删除空行

这是一个应该解决问题的最小答案。我确实将代码减少到必要的部分,因为我需要自己测试它,而且我无法访问你使用过的类。

在提问时,您应该尽可能将问题中的代码设为minimal,以便重新创建。

public static void main(String args[]) {

List lines = new ArrayList();

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值