我想从文本文件中读取由空行分隔的多个矩阵。我一步一步来。现在作为测试的一部分,我只想通过在每个矩阵之间添加"空白"字来向控制台显示矩阵。 当我运行下面的代码时,只显示每隔一行?如何识别空行?到底发生了什么?
while (scan.hasNextLine()) {
//check for blank line
if (scan.nextLine().trim().length()==0){
System.out.println("BLANK");
}else {
System.out.println(scan.nextLine());
}
}
每次该行不为空时,您都会调用nextLine
两次。
读取该行一次并将其保存在变量中
while (scan.hasNextLine()) {
//check for blank line
String val = scan.nextLine();
if (val.trim().length()==0){
System.out.println("BLANK");
}else {
System.out.println(val);
}
}