I am trying to do a telephone directory using arrays (I have to use arrays). I am trying to write the method for adding new entries. I decided to add a new entry with a line that will be split using the split
method into three parts (surname, initials, number) with tabs. When I tried to do the testing for the method I got thrown an IndexOutOfBoundsException
.
This is the addEntry
method
@Override
public void addEntry(String line) {
String[] entryLine = line.split("\\t");
String surname = entryLine[0];
String initial = entryLine[1];
String number = entryLine[2];
Entry entry = new Entry(surname, initial, number);
count++;
if (surname == null || initial == null || number == null) {
throw new IllegalArgumentException("Please fill all the required fields, [surname,initials,number]");
}
if (count == entries.length) {
Entry[] tempEntries = new Entry[2 * count];
System.arraycopy(entries, 0, tempEntries, 0, count);
entries = tempEntries;
} else {
int size = entries.length;
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < entries.length; j++) {
String one = entry.getSurname();
if (one.toLowerCase().compareTo(surname.toLowerCase()) > 0) {
Entry tempE = entries[i];
entries[i] = entries[j];
entries[j] = tempE;
}
}
}
}
}
This is the entry I tried to add:
arrayDirectory.addEntry("Smith SK 005598");