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: HELP!! Phone Directory Program

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

    Default HELP!! Phone Directory Program

    Okay...SO I have the code written. It compiles and it runs...but it doesn't do what it's supposed to -__- I realize that's not helpful so heres a description of my assignment:

    UML Diagram:

    OABlist
    -data: ABLNode[] //create private static class that takes name/number as data
    -size : int
    -capacity: int

    +OABlist () //creates an empty list with capacity = 5
    +size(): int //returns the list size
    +addorReplace (in name: String, in number : int) : boolean //adds entry according to alphabetical order of name. if node exists, update phone number without creating additional entry
    +getNumber(in name : String) : int //returns phone number corresponding to name or -1 if name not in list. Method must be faster than linear.
    +remove(in name: string) :int //returns phone number corresponding to name or -1 if name is not on the list. node storing this info is removed from list. also must be faster than linear
    +showList() : void //prints all the elements in the list (name and number)


    My code:

    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
            int j = 0;
            while (j < size){
              if (name.compareToIgnoreCase(data[j].name) >= 0){// name is bigger
                j++;
              }
              else{ 
                data[j] = new ABLNode(name, number);
                j++;
              }
            }
          }
          if(size == capacity){
            ABLNode[] newPair = new ABLNode[2 * size];
            System.arraycopy(newPair,0,data,0,capacity);
            data = newPair;
          }
      return true;
      }
     
      public int remove(String name){
        if (name != null){
          String temp = name;
          if (size == 1)
            name = null;
          else if (name == data[capacity].name) // last
            name = data[capacity - 1].name;
          else
            name = data[size].name;
            size--;
            }
        else{
          System.out.println("No one to remove");
        }
        return data[size].number;
      }
     
      public void showList(){
        int i;
        for(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();
     
      }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: HELP!! Phone Directory Program

    it doesn't do what it's supposed to
    Can you explain what it does and why that is not what you want it to do?
    Show the program's output and explain what is wrong and show what the output should be.

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

    Default Re: HELP!! Phone Directory Program

    basically, its supposed to print out the list (name, number)...however when i run it it just shows "run OABlist" but it doesn't do anything. And there are no compiler or runtime errors at all...so I was a bit lost on where I was going wrong.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: HELP!! Phone Directory Program

    Add some println statements to the code to show where the code is executing.
    Something like:
    System.out.println("Entered constructor");

    Also print out the values of variables used to control the logic flow.

    Sounds like your program must be in a loop if it does not print out anything.
    Be sure to add a println before each loop and one after each loop to show you which loop is not ending.

Similar Threads

  1. Phone Directory code
    By lahegemon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 3rd, 2011, 11:47 PM
  2. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  3. i like to play this game with my phone and i wanted to make a program to help me
    By Imreallyawesome in forum What's Wrong With My Code?
    Replies: 25
    Last Post: August 12th, 2011, 07:34 PM
  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