Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Address Book Program in java

  • 11-12-2011 3:25am
    #1
    Registered Users, Registered Users 2 Posts: 69 ✭✭


    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:
    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 apperciated :)


Comments

  • Registered Users, Registered Users 2 Posts: 339 ✭✭duffman85


    have a look at the java.io package(details here: http://docs.oracle.com/javase/6/docs/api/java/io/package-summary.html) to write to the file.

    write a program that writes and reads something to a binary(non-text) file - once you have that working, come back to your address book program.


  • Registered Users, Registered Users 2 Posts: 1,645 ✭✭✭k.p.h


    I think StreamTokenizer might be useful for you in that program, I'm not entirely sure it would work but might be worth a google.


Advertisement