Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:33:58+00:00 2026-05-27T10:33:58+00:00

I’m doing a small program an addressbook that allows the user to: add contact,

  • 0

I’m 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, (i.e. name, lastname, address and number)?
I’m terrible at Java and I need to get this done.

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 () { }
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader; // reads from dat file
import java.io.FileWriter; // writes from dat file
import java.io.IOException;
import java.io.InputStreamReader; 

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) { }
     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:33:58+00:00Added an answer on May 27, 2026 at 10:33 am

    Your file format should be a .csv, so it would look like:

    name,lastname,address,number,
    name,lastname,address,number,
    name,lastname,address,number,
    

    I know I shouldn’t be posting code for you, but here:

    class Contact {
      public String name, lastname, address, number;
    
      public Contact(String name, String lastname, String address, String number) {
        this.name = name;
        this.lastname = lastname;
        this.address = address;
        this.number = number;
      }
    
      public boolean equals(Contact c) {
        if(name.equals(c.name) && lastname.equals(c.lastname)
              && address.equals(c.address) && number.equals(c.number))
          return true;
        return false;
      }
    
      public String toString() {
        return name+","+lastname+","address+","+number+",";
      }
    }
    
    public class ContactDriver {
      public ArrayList<Contact> contacts = new ArrayList<Contact>();
    
      public static void addContact(Contact c) {
        contacts.add(c);
      }
    
      public static Contact deleteContact(Contact c) {
        return contacts.remove(c);
      }
    
      public static int searchContact(Contact c) {
        for(int i = 0; i < contacts.size(); i++)
          if(contacts.get(i).equals(c))
            return i;
        return -1;
      }
    
      public static void readContacts(String file) throws Exception {
        Scanner in = new Scanner(new File(file)).useDelimiter(",");
    
        while(in.hasNextLine()) {
          addContact(in.next(), in.next(), in.next(), in.next());
        }
      }
    
      public static void writeContacts(String fileName) {
        FileWriter dest = new FileWriter(fileName);
    
        for(Contact c : contacts)
          dest.write(c.toString());
      }
    
      public static void main() {
        readContacts();
        // Do logical stuffs
        writeContacts();
      }
    }
    

    That code is untested, so I’ll edit anything that has an error.

    Have fun learning more Java!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.