Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Phone Directory code

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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();

    }

    }
    Last edited by lahegemon; November 3rd, 2011 at 09:39 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default 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.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default 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.
    Improving the world one idiot at a time!

Similar Threads

  1. What's wrong with my code for remembering JFileChooser's Last Directory?
    By esplanade56 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2011, 08:23 AM
  2. phone book code problems
    By mu'min in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 16th, 2010, 10:36 AM
  3. phone book code problems
    By mu'min in forum Member Introductions
    Replies: 1
    Last Post: December 16th, 2010, 08:07 AM
  4. phone book
    By Skat in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 3rd, 2010, 08:56 AM
  5. [SOLVED] how to get phone time and date
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: August 26th, 2009, 11:15 AM

Tags for this Thread