-
Phone Directory code
This is what I have so far:
I need help with the addorReplace method, which adds entry alphabetically, and updates the number if the node exists. Please help! I can't get it add alphabetically!
public class OABlist{
private static class ABLNode{
public String name;
public int number;
public ABLNode (String in_name, int in_number){
name = in_name;
number = in_number;
}
}
private ABLNode[] data; //phoneentry
private int size;
private int capacity;
private static final int initial_cap = 5;
public OABlist(){
size =0;
capacity = initial_cap;
data = new ABLNode[capacity];
}
public int size() {
return size;
}
private int getName(String name) {
for (int i = 0; i < size; i++) {
if (data[i].name.equals(name)){
System.out.println(data[i].name);
return i;// The name found at index i
}
}
return -1; // The name DNE in list.
}
public int getNumber(String name) {
int j = getName(name);
if (j == -1)
return -1; // name not found
else
return data[j].number;
}
public boolean addorReplace(String name, int number){
if (name == null || number == -1)
throw new IllegalArgumentException("Name/Number are null.");
int i = getName(name);
if (i >= 0) {
data[i].number = number;// Name exists in position i, replace number.
}
else { //add new name/number to list. if list = full, create larger array
if(size == capacity){
ABLNode[] newPair = new ABLNode[2 * size];
System.arraycopy(newPair,0,data,0,capacity);
data = newPair;
}
data[size] = new ABLNode(name, number);
size++;
}
return true;
}
public void showList(){
int i;
for(i = 0; i >= 0 || i < size; i++){
System.out.println("List: " + data[i].name + " " + data[i].number );
}
}
public static void main(String[] args){
OABlist myList = new OABlist();
myList.addorReplace("Joe", 123);
myList.addorReplace("Jim", 123);
myList.addorReplace("Al", 123);
myList.addorReplace("Bill", 123);
myList.addorReplace("Carl", 123);
myList.showList();
}
}
-
Re: Phone Directory code
The "here's my code fix it for me" type of posts do not get much help. "I can't get it add alphabetically" provides zero information. What does it do instead? Are you getting any error messages? Then copy and paste the exact error message. Try and make our job as easy as possible and you are more likely to get a favourable reply.
-
Re: Phone Directory code
I'm not getting any errors with my code now. The addorReplace method is supposed to use linear complexity to add new phone entries (containing a string(name) and an int(number) ). However when I run the program, it prints the list without sorting it. I'm trying to figure out how to incorporate that into my addorReplace method but I don't know where to start.
-
Re: Phone Directory code
Consider you have a List (numbers indicate index): A(0), D(1), G(2)
Now you want to insert C
Set some counter to 0
Value at counter A is less than C
Inc counter to 1
Value at counter D is greater than C
Now we know that C has to be inserted at index 1
Insert C into list at the appropriate index
Write as many helper methods as you want to assist in doing this. Personally I would have 2 extra methods (search and insert) and all addOrReplace method does is call those 2 methods.