我目前正在开展一个学习项目,我们正在学习使用Arrays of Objects。我一直遇到两个不同的问题,我想谈谈。我正在使用NetBeans IDE以及我在下面提到的所有代码。
首先,在我向数组添加值之后,我尝试使用一个列表按钮,它应该在jtextArea中显示值。弹出没有明显的错误,但是,所有的值都说“null”而不是用户输入的值。由于有5个值,程序显示“null null null null null”。我不确定为什么会这样,任何意见都会受到赞赏。
其次,我试图允许用户根据已存储的5个值之一删除信息。在该程序的情况下,用户必须输入“员工ID”以从程序列表中删除所有所述员工的数据。我在执行此操作时遇到问题,因为您可以在下面的代码中看到。
总结一下: 如何更正此随机“空”错误,以便显示插补数据? 如何根据idNumber的用户输入有效删除给定索引的值?
要记住的事情: - 我需要使用一个对象数组 - 我对编码很新,所以请原谅我可能的无知 - 任何看似缺失的内容都可能包含在NetBeans代码中,因此可能未包含在内,这是我心中关注的主要代码
以下是代码:
//ArrayList of Objects Declared
ArrayList <employees> list = new ArrayList <employees>();
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
employees e;
//Declaring Strings
String idNumber, firstName, lastName, annualSalary, startDate;
//Reads the input fields
idNumber = idInput.getText();
firstName = fNameInput.getText();
lastName = lNameInput.getText();
annualSalary = annualInput.getText();
startDate = startDateInput.getText();
//Sends input information as a class
e = new employees(idNumber, firstName, lastName, annualSalary, startDate);
//If data is missing, the user will receive and error message
if (idNumber.isEmpty()||firstName.isEmpty()||lastName.isEmpty()||annualSalary.isEmpty()||startDate.isEmpty()){
infoOutput.setText("Please fill out all catagories!");
}
//Otherwise, the information provided will be stored
else {
list.add(e);
infoOutput.setText("");
infoOutput.setText("Employee data added!");
}
}
private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Reset infoOutput
infoOutput.setText("");
employees e;
//Declaring Strings
String idNumber, firstName, lastName, annualSalary, startDate;
//Reads the input fields
idNumber = idInput.getText();
firstName = fNameInput.getText();
lastName = lNameInput.getText();
annualSalary = annualInput.getText();
startDate = startDateInput.getText();
//Sends input information as a class
e = new employees(idNumber, firstName, lastName, annualSalary, startDate);
//Determines if the requested employee exists in the list
for (int index = 0; index < list.size(); index++) {
//If the employee ID is found, their information is removed
if (list.get(index).toString() == idNumber){
infoOutput.setText("Employee data removed!");
list.remove(index);
}
//Otherwise, an error message is displayed to the user
else{
infoOutput.setText("The employee ID " + idNumber + " was not found!");
}
}
}
class employees {
String idNumber, firstName, lastName, annualSalary, startDate;
employees(String idNumber, String firstName, String lastName, String annualSalary, String startDate) {
idNumber = idInput.getText();
firstName = fNameInput.getText();
lastName = lNameInput.getText();
annualSalary = annualInput.getText();
startDate = startDateInput.getText();
}
}
private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {
//Reset temp
String temp = "";
//List all of the stored data
for (int x = 0; x <= list.size()-1; x++) {
temp = temp + list.get(x).idNumber + " "
+ list.get(x).firstName + " "
+ list.get(x).lastName + " "
+ list.get(x).annualSalary + " "
+ list.get(x).startDate + "\n";
}
outputField.setText(temp);
}
感谢您的帮助!
答案 0 :(得分:2)
员工的构造函数不初始化实例变量,它初始化参数。例如,将idNumber初始化为
this.idNumber = idNumber;
对剩余的实例变量执行类似的语句。这就是员工类中所有值都为空的原因。
在测试idNumber时,您将使用idNumber比较员工的toString()。您必须将列表中员工的idNumber与UI中的idNumber进行比较。
if (list.get(index).idNumber.equals(idNumber)) {
最好为员工类中的每个属性创建一个访问者。
答案 1 :(得分:0)
以下是我在评论中描述的带有employees
方法的getData
类。 (我还更改了由@downyt更正的构造函数)
class employees {
String idNumber, firstName, lastName, annualSalary, startDate;
employees(String idNumber, String firstName, String lastName, String annualSalary, String startDate) {
this.idNumber = idNumber;
this.firstName = firstName;
this.lastName = lastName;
this.annualSalary = annualSalary;
this.startDate = startDate;
}
String getData(){
return idNumber + " " + firstName + " " + lastName + " " + annualSalary + " " + startDate;
}
}
使用此方法,打印代码将更改为类似于此的内容:
for (int x = 0; x < list.size(); x++){
temp = list.get(x).getData() + "\n";
}
outputField.setText(temp);
如果您愿意,也可以将"\n"
放入getData()
,将温度更改为
temp += list.get(x).getData();