【发布时间】:2015-11-28 12:19:18
【问题描述】:
我有一个file,其内容如下:
5
Derrick Rose
1 15 19 26 33 46
Kobe Bryant
17 19 33 34 46 47
Pau Gasol
1 4 9 16 25 36
Kevin Durant
17 19 34 46 47 48
LeBron James
5 10 17 19 34 47
每个名称和数字之间有一个空行。但是,当我使用 nextLine(); 扫描文件时方法我得到以下内容:
NAME:
NAME:
NAME: Derrick Rose
NAME:
NAME: 1 15 19 26 33 46
NAME:
NAME: Kobe Bryant
NAME:
NAME: 17 19 33 34 46 47
NAME:
谁能告诉我问题出现在我的代码中的什么地方以及为什么它在空白行中扫描。
Scanner scan = new Scanner(file);
int lim = scan.nextInt();
for(int i = 0; i < (lim * 2); i++)
{
String name = scan.nextLine();
System.out.println("NAME: " + name);
}
【问题讨论】:
-
它正在扫描空行,因为您的输入有空行。你提到一个错误,你能详细说明一下吗?你遇到了什么错误?
-
nextLine()逐行读取您的文件,包括空行。试试if (name.equals("")) continue;之类的东西不要打印出来。 -
@Trobbins 抱歉,我刚刚修复了它,它没有出现错误。但我的印象是扫描仪忽略了每行之间的空白行
-
@Beginner 它没有。扫描仪按原样读取输入。
标签: java string file java.util.scanner