当我添加新人时,为什么我的目录程序中的添加方法将文本文件中的前四个字段设置为空?
Why is my add method in my directory program setting the first four fields to null in my text file when I add a new person?
我有一个文本文件,其中包含我的计划中的所有人员(职员、雇员、讲师和学生)以及他们的相关信息,具体取决于人员的类型。我有一个包含我所有方法的目录 class,以及一个作为接口 class.
的目录服务器
我的添加方法无法正常工作,因为当我添加新类型的人时,它会在我尝试更新我的文本文件时将前四个字段设置为空。同样在我添加一个新人然后尝试查找(查找是我程序中的另一种方法)之后,它说该名称不在目录中,即使它在目录中。第四个字段之后的字段是用户输入的内容,在我的文本文件中正确显示。
这是我在我的目录下的添加方法和关闭方法class:
public boolean add(Person obj) throws IOException {
boolean added = true;
String s = obj.toString();
dir[directorySize++] = s;
return added;
}
public void closeDirectory() {
directoryDataIn.close();
PrintStream directoryDataOut = null;
try {
directoryDataOut = new PrintStream(directoryFile);
}
catch (FileNotFoundException e) {
System.out.printf("File %s not found, exiting!", directoryFile);
System.exit(0);
}
String originalDirectory[] = {"Staff 77778 Julie Chang Registrar","Adjunct 19778 Mike Thompson CS mtxxx@gmail.com GITC2400", "Staff 30041 Anne Mathews Security","Junior 98444 Serene Murray Math smyyy@gmail.com", "Freshman 98772 Bob Mathew CS bmyyy@gmail.com","Professor 19010 Joan Berry Math jbxxx@gmail.com GITC2315C","Professor 19871 Aparna Khat CS akxxx@gmail.com GITC1400","Adjunct 18821 Hari Mentor Physics hmxxx@gmail.com CK231","Staff 20112 Jim George Plant","Junior 68339 Tom Harry CS thyyy@gmail.com","Senior 78883 Vince Charles IT vcyyy@gmail.com","Freshman 87777 Susan Han EE shyyy@gmail.com","Senior 88888 Janki Khat IE jkyyy@gmail.com","Staff 5555 Aparna Sen Plant","Senior 66663 Jill Kaley it jk@jk.com","Staff 77777 Joe Batra plumbing","Staff 33333 Jim Natale Plumbing"};
if (originalDirectory == dir) //do not update text file if no changes were made
System.exit(0);
else
for (int i = 0; i < directorySize; i++)
directoryDataOut.println(dir[i]);
directoryDataOut.close();
}
我的文本文件的摘录应该是这样的:
Senior 88888 Janki Khat IE jkyyy@gmail.com
但结果是这样的:
null null null null MATH rwood@njit.edu
我该如何解决这个问题?
出于某种原因,这个错误的输出是今天才出现的。上周一切正常时,我关闭并保存了所有 class 文件。
这是我的人class
public class Person {
private String Position;
private String UCID;
private String FirstName;
private String LastName;
public Person(String Position, String UCID, String FirstName, String LastName) {
Position = Position;
UCID = UCID;
FirstName = FirstName;
LastName = LastName;
}
public String toString() {
return String.format("%s %s %s %s", Position, UCID, FirstName, LastName);
}
}
这是我的目录服务器 class。在这段代码摘录中,我添加了一位教授或兼职人员。在这部分之前,我只是使用扫描仪对象逐行获取每个字段的用户输入。
p = new Instructor(pos, ucid, fname, lname, dept, email, office);
try {
d.add(p);
}
catch (Exception e) {
System.out.println("Error, cannot add");
continue;
}
在您的添加方法行中:
String s = obj.toString();
可能 returns 错误 String
Person
class 的表示。您是否正确覆盖了 public string toString()
方法?
用同样的方法我也建议注意以下行:
dir[directorySize++] = s;
数组有固定的大小,这是在初始化时指定的。在未来的某个时刻,您将面临 IndexOutOfBounds
异常,因为将没有 space 用于插入新记录。我建议改用 LinkedList
,它可以用新记录动态扩大。
总而言之,如果没有看到 Person
class 和负责填充文本文件的代码,很难为您的问题给出指定的解决方案
问题出在人身上 class。您需要使用如下语句:
this.Position = Position
否则你只是将局部变量分配给局部变量。
此外,出于文体原因,您希望实例变量(字段)为小写。
我有一个文本文件,其中包含我的计划中的所有人员(职员、雇员、讲师和学生)以及他们的相关信息,具体取决于人员的类型。我有一个包含我所有方法的目录 class,以及一个作为接口 class.
的目录服务器我的添加方法无法正常工作,因为当我添加新类型的人时,它会在我尝试更新我的文本文件时将前四个字段设置为空。同样在我添加一个新人然后尝试查找(查找是我程序中的另一种方法)之后,它说该名称不在目录中,即使它在目录中。第四个字段之后的字段是用户输入的内容,在我的文本文件中正确显示。
这是我在我的目录下的添加方法和关闭方法class:
public boolean add(Person obj) throws IOException {
boolean added = true;
String s = obj.toString();
dir[directorySize++] = s;
return added;
}
public void closeDirectory() {
directoryDataIn.close();
PrintStream directoryDataOut = null;
try {
directoryDataOut = new PrintStream(directoryFile);
}
catch (FileNotFoundException e) {
System.out.printf("File %s not found, exiting!", directoryFile);
System.exit(0);
}
String originalDirectory[] = {"Staff 77778 Julie Chang Registrar","Adjunct 19778 Mike Thompson CS mtxxx@gmail.com GITC2400", "Staff 30041 Anne Mathews Security","Junior 98444 Serene Murray Math smyyy@gmail.com", "Freshman 98772 Bob Mathew CS bmyyy@gmail.com","Professor 19010 Joan Berry Math jbxxx@gmail.com GITC2315C","Professor 19871 Aparna Khat CS akxxx@gmail.com GITC1400","Adjunct 18821 Hari Mentor Physics hmxxx@gmail.com CK231","Staff 20112 Jim George Plant","Junior 68339 Tom Harry CS thyyy@gmail.com","Senior 78883 Vince Charles IT vcyyy@gmail.com","Freshman 87777 Susan Han EE shyyy@gmail.com","Senior 88888 Janki Khat IE jkyyy@gmail.com","Staff 5555 Aparna Sen Plant","Senior 66663 Jill Kaley it jk@jk.com","Staff 77777 Joe Batra plumbing","Staff 33333 Jim Natale Plumbing"};
if (originalDirectory == dir) //do not update text file if no changes were made
System.exit(0);
else
for (int i = 0; i < directorySize; i++)
directoryDataOut.println(dir[i]);
directoryDataOut.close();
}
我的文本文件的摘录应该是这样的:
Senior 88888 Janki Khat IE jkyyy@gmail.com
但结果是这样的:
null null null null MATH rwood@njit.edu
我该如何解决这个问题? 出于某种原因,这个错误的输出是今天才出现的。上周一切正常时,我关闭并保存了所有 class 文件。
这是我的人class
public class Person {
private String Position;
private String UCID;
private String FirstName;
private String LastName;
public Person(String Position, String UCID, String FirstName, String LastName) {
Position = Position;
UCID = UCID;
FirstName = FirstName;
LastName = LastName;
}
public String toString() {
return String.format("%s %s %s %s", Position, UCID, FirstName, LastName);
}
}
这是我的目录服务器 class。在这段代码摘录中,我添加了一位教授或兼职人员。在这部分之前,我只是使用扫描仪对象逐行获取每个字段的用户输入。
p = new Instructor(pos, ucid, fname, lname, dept, email, office);
try {
d.add(p);
}
catch (Exception e) {
System.out.println("Error, cannot add");
continue;
}
在您的添加方法行中:
String s = obj.toString();
可能 returns 错误 String
Person
class 的表示。您是否正确覆盖了 public string toString()
方法?
用同样的方法我也建议注意以下行:
dir[directorySize++] = s;
数组有固定的大小,这是在初始化时指定的。在未来的某个时刻,您将面临 IndexOutOfBounds
异常,因为将没有 space 用于插入新记录。我建议改用 LinkedList
,它可以用新记录动态扩大。
总而言之,如果没有看到 Person
class 和负责填充文本文件的代码,很难为您的问题给出指定的解决方案
问题出在人身上 class。您需要使用如下语句:
this.Position = Position
否则你只是将局部变量分配给局部变量。
此外,出于文体原因,您希望实例变量(字段)为小写。