java - 在 Java 中找不到行
问题描述
我无法从文件中读取数据。我收到的错误是:
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)
我不明白为什么当明显更多时找不到该行。我的教授说通过在读取最后一行( String temp)后创建一个临时变量来保存新行来清除缓冲区。我仍然得到同样的错误。清晰度将不胜感激。对不起,高级的长帖子。想展示所有工作以帮助大家理解我的问题。
这是我的驱动程序类:
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
解决方案
您的代码中需要更改一些小东西。以下是您的代码以及我的更正。代码后的解释。
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
File source = new File("pets.dat");
List<Pets> list = new ArrayList<Pets>();
try (Scanner kb = new Scanner(source)) {
while (kb.hasNextLine()) {
String name = kb.nextLine();
String owner = kb.nextLine();
String species = kb.nextLine();
String breed = kb.nextLine();
String ageStr = kb.nextLine();
double age = Double.parseDouble(ageStr);
String weightStr = kb.nextLine();
double weight = Double.parseDouble(weightStr);
Pets pet = new Pets(name, owner, species, breed, age, weight);
list.add(pet);
}
}
catch (IOException xIo) {
xIo.printStackTrace();
}
System.out.println("count = " + list.size());
}
}
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;
}
}
由于您的问题是读取文件,因此我只是编写了一个main()
方法来演示您应该如何读取文件。
请注意,while
循环条件是kb.hasNextLine()
而不是kb.hasNext()
。您的数据文件每行包含一项数据。因此,您需要一次阅读一行。nextLine()
这就是为什么我只在while
循环内调用方法。您需要阅读一行并处理它。因此,我最初读取包含 adouble
作为字符串的行,然后将该字符串转换为双精度。我从您的代码中删除了这一行:
String temp = kb.nextLine();
这不是必需的。我认为您的教授试图告诉您将年龄和体重读取为字符串,然后将每个字符串转换为双精度,就像我所做的那样。这就是为什么他建议使用临时变量。
此外,我将 a 存储Pets
在 a而不是数组中,因为(理论上)a可以包含List
多少个元素没有限制。List
阅读完所有内容后,关闭数据文件也很重要。这就是我使用try-with-resources的原因。
推荐阅读
- jquery - Jquery Selector - 查找 div 是否有内文
- c# - 根据分数提高球员的速度?
- python - 将返回多个地图对象的函数转换为单个数组
- logic-programming - Pydatalog,无法打印规则的右侧
- python - 如何根据两个不同列中存在的值转置 DataFrame?
- python - Pandas - DataFrames之间的映射值
- reactjs - 将 FastImage 与我的 react 本机应用程序一起使用时出错
- django - Django/Graphene Mutation 错误“您需要传递有效的 Django 模型”无法修复
- google-chrome-extension - 如何使用 SockJS 连接到服务器
- android - 无法在工作线程上同步获取 firebase id 令牌