Results 1 to 2 of 2
Thread: Address Book Program in java
- 12-11-2011, 04:39 AM #1
Member
- Join Date
- Dec 2011
- Location
- Ireland
- Posts
- 1
- Rep Power
- 0
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){} } }
- 12-11-2011, 04:59 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
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
-
Help deleting contacts in address book
By abatakji74 in forum AWT / SwingReplies: 0Last Post: 05-11-2011, 11:33 PM -
Help with JAVA Address Book Demo
By Radu in forum AWT / SwingReplies: 4Last Post: 04-27-2011, 03:29 PM -
hi, i want to access the address book of yahoo,gmail from java
By madhumca_k in forum New To JavaReplies: 1Last Post: 02-17-2010, 07:28 AM -
java address book problems
By Ekul in forum AWT / SwingReplies: 3Last Post: 11-11-2009, 08:14 PM -
Programming an Address book
By d.anthonii in forum New To JavaReplies: 0Last Post: 12-31-2007, 06:26 PM
Bookmarks