我有文本 .dat 文件,我从我的主类加载这个文件并读入我的 DataReader 类。但我收到错误,我必须将修饰符更改为静态。我不能这样做,因为它必须是非静态的。
我被困在这里,不起诉我的问题是在这里还是其他地方。你会检查我的代码并让我知道它是否可以吗?下一行也不存储在车辆中并显示为空!!
此代码得到错误:
if(DataReader.loadData(args[0])) { // i get errors here
并要求我将其更改为:(public static boolean loadData(String VehicleData) { /// but this code has to be non-static...
我的教授要求)
主类:
public class Project3 {
private static Vehicle[] vehicles;
static int x;
public static void main(String[] args) {
// Display program information
DataReader reader = new DataReader(); // The reader is used to read data from a file
// Load data from the file
**if(DataReader.loadData(args[0]))** { // i get errors here
vehicles= reader.getVehicleData(); // this line also shows null
// Display how many shapes were read from the file
System.out.println("Successfully loaded " + vehicles[0].getCount() +
" vehicles from the selected data file!");
displayMenu();
}
}
数据读取器类:
ublic boolean loadData(String VehicleData) {
boolean validData = false;
String line;
try{
// Open the file
BufferedReader reader = new BufferedReader(new FileReader("VehicleData.dat"));
//Read File Line by line
while((line=reader.readLine()) !=null) {
addVehicle(line.split(","));
}
reader.close();
vehicles = Array.resizeArray(vehicles, vehicleCount);
validData = true;
}