Results 1 to 2 of 2
  1. #1
    DaveNAchill is offline Member
    Join Date
    Dec 2011
    Location
    Ireland
    Posts
    1
    Rep Power
    0

    Default Address Book Program in java

    Hi im looking for help doing a small program, an addressbook that allows the user to:

    add contact, search contact and delete contact.

    all of this data is read and written to .dat file.

    Also how would you create a layout in the data file, ie name,lastname,address and number?

    here is some of my code:

    Java Code:
    public interface Inter
    {
    //Interface class 
    
    
        public void addContact();
        public void deleteContact();
        public void searchContact();
        public void readFile();
    }
    
    public class Contact
    {
    
        static String name;
        static String lastName;
        static String address;
        static String number;
    
    
    
    
        public Contact ()
        {
    
    
        }
    
    
    }
    
    
    
    
    public class Phonebook extends Contact implements Inter
        {
    
    
            public static void main(String[] args)
            {
    
    
            } // main
    
    
    
            @Override
            public void deleteContact()
            {
    
    
            }
    
            @Override
            public void searchContact() 
            {
    
    
            }
    
    
    
            @Override
            public void addContact() 
            {
    
                String details = null;
                System.out.println("Enter new contact i.e name:number:lastname ");
                InputStreamReader converter = new InputStreamReader(System.in);
                BufferedReader in = new BufferedReader(converter);
    
                try
                {
                    details=in.readLine();
                    String[] tokens =details.split(":"); // eg david :098:Needham
                    name= tokens[0];
                    lastName = tokens[1];
                    address = tokens[2];
                    number = tokens[3];
    
    
    
    
    
    
    
                } 
    
                catch (IOException e1) 
                {
    
                }
    
    
    
    
    
                    FileWriter fw = null; // writes contact info to the dat file
                    try 
                    {
                        fw = new FileWriter("data.dat");
                        fw.write(name);
                        fw.write(lastName);
                        fw.write(address);
                        fw.write(number);
    
                    } catch (IOException e) {
    
                    }
                    BufferedWriter bw = new BufferedWriter(fw);
    
    
    
            }
    
    
    
    
    
    
            public void readFile() // reads contacts from dat file
            {
    
                try
                {
                    BufferedReader in = new BufferedReader(new FileReader("data.dat"));
                     String str;
                        while ((str = in.readLine()) != null) 
                        {
    
    
                        }
    
                 }
    
    
                catch(Exception ex){}
    
    
    
            }
        }
    Help is much apperciated!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Address Book Program in java

    Why does Phonebook extend Contact? What I mean is that a phonebook is not a type of contact. And why are name, lastName etc static? A good rule of thumb is not to have anything static apart from main() methods that serve as small entry points to a runnable program.

    (A mistake you see sometimes is to use "static" and "extends" as a way of keeping the compiler quiet. But compiler messages are good things, and what they report needs to be understood.)

    -----

    It might be worth thinking about and documenting the various parts of your program before rushing into code. What do Phonebook, Contact and Inter represent? Phonebook implements Inter, but what is that behaviour - ie what is the intended behaviour of each of the Inter methods? Does a Contact instance have any behaviour?

    -----

    To address the specific question you ask, I don't think it matters too much to begin with how you represent the contacts within the file. Since you are already asking the user to enter the data in a colon delimited format you could choose the same for how you write the data to file. Simply running the data together as you do currently with fw.write()/fw.write()/fw.write() could lead to problems as you complete the code in the while loop of readFile().

Similar Threads

  1. Help deleting contacts in address book
    By abatakji74 in forum AWT / Swing
    Replies: 0
    Last Post: 05-11-2011, 10:33 PM
  2. Help with JAVA Address Book Demo
    By Radu in forum AWT / Swing
    Replies: 4
    Last Post: 04-27-2011, 02:29 PM
  3. Replies: 1
    Last Post: 02-17-2010, 06:28 AM
  4. java address book problems
    By Ekul in forum AWT / Swing
    Replies: 3
    Last Post: 11-11-2009, 07:14 PM
  5. Programming an Address book
    By d.anthonii in forum New To Java
    Replies: 0
    Last Post: 12-31-2007, 05:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •