我被要求以数组的形式创建一个职员电话簿,该电话簿将保留其姓名和分机号。电话簿支持多种功能:
添加将按姓氏的字母顺序插入的新条目
按名称或数字删除条目
给定名称的查找名称
更改号码
打印整个电话簿
所以没有任何改进,但是我遇到的问题在于文本文件并无法将信息正确存储在数组中。文本文件包含用以下格式的选项卡分隔的数据:
姓氏<-TAB->首字母<-TAB->扩展名
我相信从分配的措辞来看,首先要求我将填充的文本文件数据加载到程序中的数组中,然后再使用前面提到的功能对其进行操作。他们明确表示,此处无需写回文件。
到目前为止,我已经创建了几个类,其中包含将在项目中使用的每个对象的常规信息(即Entry类,Directory接口[必需],ArrayDirectory类,DirectoryFile类)。我试图从文本文件中读取3个不同的项目,然后将其存储在为其创建构造函数的Entry对象中。我知道我将如何使用这些方法,我只是无法在文本文件和数组之间创建此初始链接,这意味着我还无法继续。我曾尝试实施在线找到的解决方案,但事实证明这更加令人困惑。
自从我将数据保存为自定义对象(条目)以来,我在这里也遇到了问题,并最终感到困惑。
cursor.arrayvar(cx_Oracle.NUMBER, [1, 2, 3])
public class Entry {
private String firstName = null;
private String surname = null;
private String extension = null;
public Entry(String firstName, String surname, String extension) {
super();
this.firstName = firstName;
this.surname = surname;
this.extension = extension;
}
...(getters and setters, toString)...
public class ArrayDirectory implements Directory {
private final static int MAX_ENTRIES = 20;
private static Scanner input = new Scanner(System.in);
DirectoryFile file = new DirectoryFile("C:\\Users\\John\\Documents\\Phonebook.txt");
static List<Entry> phonebook = new ArrayList<>(MAX_ENTRIES);
public void addEntry() throws IOException {
// check if phone book is empty before loading entries
System.out.println(phonebook.isEmpty());
loadEntries(file.getFile());
System.out.println("Enter first name: ");
String fname = input.nextLine();
System.out.println("Enter second name: ");
String sname = input.nextLine();
System.out.println("Enter telephone extension: ");
String telephone = input.nextLine();
Entry entry = new Entry(fname, sname, telephone);
phonebook.add(entry);
System.out.println("Added staff member: \n" + fname + "\n" + sname + "\n" + telephone);
System.out.println(Arrays.toString(phonebook.toArray()));
}
public String loadEntries(File file) throws IOException {
String surname = null, fname = null, extension = null;
BufferedReader br = new BufferedReader(new FileReader(file));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
String arr[] = line.split("\\s+");
surname = arr[0];
fname = arr[1];
extension = arr[2];
Entry entry = new Entry(surname, fname, extension);
phonebook.add(entry);
line = br.readLine();
}
} finally {
br.close();
}
return null;
}
...(other methods for functionality, testing etc.)...
我遇到的问题是,在运行loadEntries()方法时,它给出了OutOfBoundsException并指出索引1超出长度1的范围。我想我可能也有方法的返回签名不正确,但这使我更加困惑。我应该将电话簿ArrayList传递给方法,而不要返回String吗?
对不起,这是我有史以来第一篇在线寻求帮助的帖子,如果我重复我自己,我深表歉意。
答案 0 :(得分:0)
我现在没有很多时间,但这是我为你所做的……
修改您的Entry
类以实现Comparable
并像这样覆盖equals():
public class Entry implements Comparable {
private String firstName = null;
private String surname = null;
private String extension = null;
public Entry(String firstName, String surname, String extension) {
this.firstName = firstName;
this.surname = surname;
this.extension = extension;
}
@Override
public int compareTo(Object arg0) {//important for sorting
this.surname.compareTo(((Entry) arg0).surname);
return 0;
}
@Override
public boolean equals(Object arg0) {//important for deleting
return ((Entry) arg0).firstName.equalsIgnoreCase(this.firstName);
}
}
以下是其他裁员方法:
public void writetofile(List<Entry> entry) throws Exception {
File f = new File("path");
try (Writer w = new FileWriter(f); PrintWriter pw = new PrintWriter(w);) {
for (Entry e : entry) {
String line = e.surname + "--------" + e.firstName + "--------" + e.extension;
pw.println(line);
}
} catch (Exception e) {
throw e;
}
}
public List<Entry> sortbysurname(List<Entry> entry) {
Collections.sort(entry);
return entry;
}
// returns the index of the entry
public boolean deleteByName(String name, List<Entry> entry) {
return entry.remove(new Entry(name, "", ""));
}