在Java中找不到行
我在从文件中读取数据时遇到问题。我收到的错误是:在Java中找不到行,java,file,Java,File,我在从文件中读取数据时遇到问题。我收到的错误是: Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at Driver.readFile(Driver.java:20) at Driver.main(Driver.java:9) 我不明
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Driver.readFile(Driver.java:20)
at Driver.main(Driver.java:9)
我不明白为什么没有找到线,当有明显的更多。我的教授说,在读取最后一行(stringtemp)后,通过创建一个临时变量来保存新行,从而清除缓冲区。我仍然会犯同样的错误。请澄清。很抱歉在advanced中发了这么长的帖子。想展示所有的作品来帮助大家理解我的问题
这是我的驾驶员课程:
如果需要,这里是我的宠物课程:
这是数据文件:
您的代码中需要更改一些小东西。下面是你的代码和我的更正。代码后面的解释
导入java.io.File;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公务舱司机{
公共静态void main(字符串[]args){
文件源=新文件(“pets.dat”);
列表=新的ArrayList();
尝试(扫描仪kb=新扫描仪(源)){
while(kb.hasNextLine()){
字符串名称=kb.nextLine();
字符串所有者=kb.nextLine();
字符串species=kb.nextLine();
字符串breed=kb.nextLine();
字符串ageStr=kb.nextLine();
double age=double.parseDouble(ageStr);
字符串weightStr=kb.nextLine();
double weight=double.parseDouble(weightStr);
宠物宠物=新宠物(名称、主人、物种、品种、年龄、体重);
列表。添加(pet);
}
}
捕获(IOXIO异常){
xIo.printStackTrace();
}
System.out.println(“count=“+list.size());
}
}
班级宠物{
私有字符串名称;
私人字符串所有者;
私有串种;
私家犬;
私人双倍年龄;
私人双倍重量;
宠物(){
}
宠物(绳名、绳主、绳种、绳种、双倍年龄、双倍体重){
this.name=名称;
this.owner=所有者;
这个物种=物种;
这个品种;
这个。年龄=年龄;
重量=重量;
}
布尔isSpecies(字符串物种){
if(this.species==物种){
返回true;
}
返回false;
}
公共字符串toString(){
返回“名称:”+名称+“\n”+”所有者:“+所有者+”\n“+”物种:“+物种+”\n“+”品种:“+品种+”\n“+”年龄:“+年龄+”\n“+”体重:“+体重;
}
}
由于您的问题是读取文件,因此我只编写了一个main()
方法来演示如何读取文件
请注意,while
循环条件是kb.hasNextLine()
,而不是kb.hasNext()
。您的数据文件每行包含一项数据。因此,您需要一次读一行。这就是为什么我只在while
循环中调用方法nextLine()。你需要读一行并处理它。因此,我首先将包含double
的行作为字符串读取,然后将该字符串转换为double。我从你的代码中删除了这一行:
String temp=kb.nextLine();
这不是必需的。我想你的教授是想告诉你把年龄和体重读成字符串,然后把每个字符串转换成双精度,就像我做的那样。这就是他提出临时变量的原因
此外,我将Pets
存储在列表中,而不是存储在数组中,因为列表中可能包含的元素数量(理论上)没有限制
此外,读取完数据文件的所有内容后,关闭数据文件也很重要。这就是我使用的原因。这真的是您的数据文件吗,例如,没有最后一行文本?您的扫描仪似乎与数据不一致。问题是您使用了错误的扫描仪构造函数。您的第一次nextLine
调用返回字符串“pets.dat”。它不会打开名为“pets.dat”的文件。尝试新建文件(“pets.dat”)
而不是pets.dat
。并摆脱temp
调用,因为它将吞噬下一个name
。仅供参考,如果您在收到后打印了名称
,您就会看到错误。
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Pets [] pets = new Pets[25];
int numPets = readFile(pets);
System.out.println(numPets);
}
public static int readFile(Pets [] pets) {
Scanner kb = new Scanner("pets.dat");
int count = 0;
while (kb.hasNext()) {
String name = kb.nextLine();
String owner = kb.nextLine();
String species = kb.nextLine();
String breed = kb.nextLine();
double age = kb.nextDouble();
double weight = kb.nextDouble();
String temp = kb.nextLine();
Pets pet = new Pets(name, owner, species, breed, age, weight);
pets [count] = pet;
count++;
}
return count;
}
}
public class Pets {
private String name;
private String owner;
private String species;
private String breed;
private double age;
private double weight;
Pets() {
}
Pets(String name, String owner, String species, String breed, double age, double weight) {
this.name = name;
this.owner = owner;
this.species = species;
this.breed = breed;
this.age = age;
this.weight = weight;
}
boolean isSpecies(String species) {
if (this.species == species) {
return true;
}
return false;
}
public String toString() {
return "Name: " + name + "\n" + "Owner: " + owner + "\n" + "Species: " + species + "\n" + "Breed: " + breed + "\n" + "Age: " + age + "\n" + "Weight: "+ weight;
}
}
Izzy
Liz Davidson
cat
domestic shorthair
2
10.5
Duncan
Sofia Berrios
dog
basset hound
9
56
Otis Redding
Scott Grasso
hamster
golden
0.5
0.1
Guinness
Kumar Chaudhuri
dog
mutt
6
32
Snape
Liping Xiang
snake
grass snake
3.5
0.5
Artemis
Ali Khan
cat
domestic shorthair
11
12
KD
Rachel Kauffman
dog
Akita
6
65
Morgan
Nick McEntire
cat
Russian blue
.8
8.4
Max
Kelly Luiselli
dog
mutt
15
38
Ingrid
Terry Dumas
snake
boa constrictor
3.5
4.6