我正在尝试从文件导入数据并从中创建对象,然后将其存储到数组中。我是初学者,我不确定我做错了什么。
我收到了6个错误:
。\ Employees.java:92:错误:预期的类,接口或枚举 } ^
Employeetest.java:24:错误:找不到符号 objectArray [counter] = setFirstName(firstNameEmployee);
symbol:方法setFirstName(String) 位置:类Employeetest Employeetest.java:26:错误:找不到符号 objectArray [counter] = setLastName(lastNameEmployee);
symbol:方法setLastName(String) 位置:类Employeetest Employeetest.java:28:错误:找不到符号 objectArray [counter] = setAge(ageEmployee);
symbol:方法setAge(double) 位置:类Employeetest Employeetest.java:30:错误:找不到符号 objectArray [counter] = setHourlySalary(hourlySalaryEmployee);
symbol:方法setHourlySalary(double) 位置:类Employeetest Employeetest.java:32:错误:找不到符号 objectArray [counter] = setWeeklyHoursWorked(weeklyhoursworkedEmployee);
symbol:方法setWeeklyHoursWorked(double) 位置:类Employeetest 6个错误
这就是我所拥有的。
This is my objects class
public class Employees {
private String firstName;
private String lastName;
private double age;
private double hourlySalary;
private double weeklyhoursworked;
private double weeklysalary;
public void setFirstName(String EmployeeFirstName) {
firstName = EmployeeFirstName;
return;
}
public void setLastName(String EmployeeLastName){
lastName = EmployeeLastName;
return;
}
public void setAge(double EmployeeAge){
age = EmployeeAge;
return;
}
public void setHourlySalary(double EmployeeHourlySalay){
hourlySalary = EmployeeHourlySalay;
calculateWeeklySalary(weeklyhoursworked);
return;
}
public void setWeeklyHoursWorked(double EmployeeWeeklyHoursWorked){
weeklyhoursworked = EmployeeWeeklyHoursWorked;
return;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public double getAge(){
return age;
}
public double getHourlySalary(){
return hourlySalary;
}
public double getWeeklyHourlyWorked(){
return weeklyhoursworked;
}
public double getWeeklySalary(){
return weeklysalary;
}
public void calculateWeeklySalary(double hoursWorked) {
weeklysalary = (hoursWorked * hourlySalary);
return;
}
}
这是我的主要程序,我尝试导入数据,将其转换为对象,然后将其存储为数组。
import java.io.*;
public class Employeetest
{
public static void main(String[] args) throws Exception
{
String firstNameEmployee = "null";
String lastNameEmployee = "null";
double ageEmployee;
double hourlySalaryEmployee;
double weeklyhoursworkedEmployee;
double weeklySalaryEarnedEmployee;
int counter = 0;
Employees objectArray[] = new Employees[counter + 1];
FileReader fr = new FileReader("Employeedata.txt");
BufferedReader br = new BufferedReader(fr);
counter = 0;
while ((firstNameEmployee = br.readLine()) != null) {
objectArray[counter] = setFirstName(firstNameEmployee);
lastNameEmployee = br.readLine();
objectArray[counter] = setLastName(lastNameEmployee);
ageEmployee = Double.parseDouble(br.readLine());
objectArray[counter] = setAge(ageEmployee);
hourlySalaryEmployee = Double.parseDouble(br.readLine());
objectArray[counter] = setHourlySalary(hourlySalaryEmployee);
weeklyhoursworkedEmployee = Double.parseDouble(br.readLine());
objectArray[counter] = setWeeklyHoursWorked(weeklyhoursworkedEmployee);
System.out.println(objectArray[counter]);
counter++;
}
br.close();
System.exit(0);
}
}
无法识别的错误,有什么值得注意的吗?
答案 0 :(得分:0)
问题
您正在调用在不先访问对象的情况下无法访问的方法..
继续前进:
while ((firstNameEmployee = br.readLine()) != null) {
objectArray[counter] = setFirstName(firstNameEmployee);
lastNameEmployee = br.readLine();
objectArray[counter] = setLastName(lastNameEmployee);
ageEmployee = Double.parseDouble(br.readLine());
objectArray[counter] = setAge(ageEmployee);
hourlySalaryEmployee = Double.parseDouble(br.readLine());
objectArray[counter] = setHourlySalary(hourlySalaryEmployee);
weeklyhoursworkedEmployee = Double.parseDouble(br.readLine());
objectArray[counter] = setWeeklyHoursWorked(weeklyhoursworkedEmployee);
System.out.println(objectArray[counter]);
counter++;
}
我们将看看这个代码块。 首先,从文件中读取值并将其分配给变量。然后,您将其分配给对象...问题变得明显的地方
这就是你拥有的
objectArray[counter] = setLastName(lastNameEmployee);
要修复,只需将具有上述方法的所有行更改为此
objectArray[counter].setLastName(lastNameEmployee);
另一个例子
而不是
objectArray[counter] = setAge(ageEmployee);
做
objectArray[counter].setAge(ageEmployee);
修复空指针异常
for(int x =0; x< objectArray.length;x++)
{
objectArray[x] = new Employees();
}
将该循环置于
之下BufferedReader br = new BufferedReader(fr);
原因是因为对象数组中的对象最初是null。因此,在你可以改变它们的实例变量之前,你需要实例化它们。