[英]Printing first line of text file
我使用扫描仪方法为我的' MyZoo '类读取制表符分隔的文本文件,如下所示:
import shipment of April 12, 2007
# tab separated data
Bird Golden Eagle Eddie
Mammal Tiger Tommy
Mammal Lion Leo
Mammal Elephant Eddie
Bird Parrot Polly
# last one next
Reptile Cobra Colin
我已经拆分了三个数据列: Type, Species and name
。
然而, 第一行数据不是动物数据 ,所以我在while循环之前使用“scanner.nextLine”来忽略它,但是我仍然希望它被打印到终端 ,而不是被读作动物数据。
码:
scanner.nextLine();
while(scanner.hasNextLine())
{
String type = scanner.next();
String species = scanner.next();
String name = scanner.next();
System.out.println(scanner.nextLine());
System.out.println(type + " " + species + " " + name);
scanner.nextLine();
addAnimal( new Animal(species, name, this) );
String line = scanner.nextLine();
if(line.startsWith("#"))
{
continue;
}
else
{
scanner.nextLine();
addAnimal( new Animal(species, name, this) );
}
}
只需更改您的初始nextLine
调用即可将其打印出来:
System.out.println(scanner.nextLine()); // Change here
while(scanner.hasNextLine())
{
String type = scanner.next();
String species = scanner.next();
String name = scanner.next();
System.out.println(scanner.nextLine());
System.out.println(type + " " + species + " " + name);
scanner.nextLine();
addAnimal( new Animal(species, name, this) );
String line = scanner.nextLine();
if(line.startsWith("#"))
{
continue;
}
else
{
scanner.nextLine();
addAnimal( new Animal(species, name, this) );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.