Jump to content

New to Programming - Trouble with (very) basic Phonebook

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Diplo

Diplo

    Newbie

  • Members
  • Pip
  • 2 posts
I know I haven't quite understood Array's with this code but am very new to programming so would appreciate if someone could tell me exactly what's wrong. Also, where there is unnecessary/bad programming.

Thanks in advance

public class Contact

{

    private String firstName;

    private String surname;

    private String address;

    private String telNumber;

    private String emailAdd;

    

    public Contact(String firstName, String surname,

        String address, String telNumber, String emailAdd)

        {

            this.firstName = firstName;

            this.surname = surname;

            this.address = address;

            this.telNumber = telNumber;

            this.emailAdd = emailAdd;

        }

        

   public String getFirstName()

   {

       return firstName;

    }

    

    public String getSurname()

    {

        return surname;

    }

    

    public String getAddress()

    {

        return address;

    }

    

    public String getTelNumber()

    {

        return telNumber;

    }

    

    public String getEmailAdd()

    {

        return emailAdd;

    }

    

    public String printDetails()

    {

        String s = new String("First Name: " + firstName + "\nSurname: " + surname + 

        "\nAddress: " + address + "\nTelephone Number: " + telNumber +

        "\nEmail: " + emailAdd + "\n");

        return s;

    

    }

}


public class AddressBook

{

    private Contact[] addBook;

    private static int counter = 1;

    

    private void AddressBook()

    {

        Contact[] addBook = new Contact[100];

        counter = 1;

    }

    

    public void AddToAddBook(Contact name)

    {   

        Contact theName = name;

        addBook[counter] = theName;

        counter++;        

    }

    

    public Contact[] ShowAddBook()

    {

        return addBook;

    }

}


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Well, it compiles. However, there's no main method, so how are you planning to run it?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Diplo

Diplo

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks for reply.

Sorry, forgot to mention that I was using a program called BlueJ so there's no need for a main method. It compiles, but I get an error saying the address book array is null when i try to view it.

EDIT: I also need to delete contacts that have been added. How can I go about this? Do I use an interator?

Edited by Diplo, 20 June 2010 - 04:23 PM.


#4
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
    private Contact[] addBook;
    private static int counter = 1;
    
    private void AddressBook()
    {
        Contact[] addBook = new Contact[100];
        counter = 1;
    }
    
    public void AddToAddBook(Contact name)
    {   
        //LOOK HERE
        Contact theName = new Contact();
        Contact theName = name;
        addBook[counter] = theName;
        counter++;
} 

Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#5
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

Davide said:

    public Contact(String firstName, String surname,
        String address, String telNumber, String emailAdd)
        {
            this.firstName = firstName;
            this.surname = surname;
            this.address = address;
            this.telNumber = telNumber;
            this.emailAdd = emailAdd;
       }
Ok, what's the point of this awesome constructor if you don't use it?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#6
InfiniteRecursion

InfiniteRecursion

    Newbie

  • Members
  • Pip
  • 5 posts
I think you have to pass in value into the parameter because he did not specific any overloaded constructor.
So what you are saying is that when you try to evoke the method ShowAddBook you get an array null error?? if that's the case then you must first have element inside your array first before you can actually access them.

to delete a contact, you can look at java api there should be a method that will remove an element inside your array.

#7
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
Arrays in java start at 0, your filling the array with from the index 1.

Also you've declared a static array size, 100, but initially all Contacts will be null; which means when you expose that array you need to expliciently check for the null values.
I'm not sure this is what you want to do. Maybe look into using a Collection (ArrayList,Vector etc) instend of an array?

#8
InfiniteRecursion

InfiniteRecursion

    Newbie

  • Members
  • Pip
  • 5 posts
I completely agree with abzero with ArrayList. Because since your project does not contain too huge of contact list, so it is ideally smart to use arraylist the size will automatic increment. Arraylist also contain many useful method and one of them will help you delete the element in the list :p