为什么我的BufferedReader不起作用?我正在尝试使用BufferedReader读取文本文件,但是每次运行该程序时,它都会在我的行中显示错误:
BufferedReader br = new BufferedReader(new FileReader("playerinfo.txt"));
我仍在学习,而且我不知道BufferedReader出了什么问题。我正在尝试使其读取玩家统计信息的文本文件,然后输出该文件。我做了一个文本文件以进行读取,但是每次尝试运行程序时仍然会出现错误。我试图查找与我类似的问题,但是似乎有几种方法可以执行BufferedReader,我认为我的方法看起来应该可以工作。我在代码中找不到其他任何看似错误的部分,但我还是很新。非常感谢您对此问题的任何帮助。
这是我尝试实施的方法:
import java.util.*;
import java.io.*;
class Player { //Base class
String name;
float weight;
int age;
Player() {
}
;
Player(String name, float weight, int age) {
this.name = name;
this.weight = weight;
this.age = age;
}
}
class Defensive extends Player {
int tackles, interceptions;
float sacks;
Defensive() {
super();
}
Defensive(String name, float weight, int age, int tackles, int interceptions, float sacks) {
super(name, weight, age);
this.tackles = tackles;
this.interceptions = interceptions;
this.sacks = sacks;
}
public String toString() {
return "name : " + name + ", weight : " + weight + ", age : " + age + ", tackles : " + tackles + ", sacks : " + sacks + ", interceptions : " + interceptions + "\n";
}
}
class Football { //main class
public Defensive readDefensive(BufferedReader br) throws IOException {
String name = br.readLine();
float weight = Float.parseFloat(br.readLine());
int age = Integer.parseInt(br.readLine());
int tackles = Integer.parseInt(br.readLine());
float sacks = Float.parseFloat(br.readLine());
int interceptions = Integer.parseInt(br.readLine());
return (new Defensive(name, weight, age, tackles, interceptions, sacks));
}
public static void main(String[] args) throws IOException {
Football football = new Football();
BufferedReader br = new BufferedReader(new FileReader("playerinfo.txt"));
String line = br.readLine();
Defensive defensive = new Defensive();
ArrayList al = new ArrayList();
while (line != null) {
if (line.equals("defensive")) {
defensive = football.readDefensive(br);
al.add(defensive);
}
line = br.readLine();
}
System.out.println(al);
}
}
答案 0 :(得分:0)
您的包含主方法的足球类应声明为public class
。
此外,当实现立即产生警告时,您还应考虑使用泛型列表。
使用List<Defensive> defensives = new ArrayList<>();