Jul 25, 2008

Building An Address Book With Java

Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

free web hosting

Building An Address Book With Java

arijitsinha99
Dear Friends
I am sharing a java program with all of u which I built a few years back. I guess it will help beginners. It uses java utill package to store data in a flat file.
The File Name-----phone.java
CODE
import java .io .*;
import java .util .*;
import java.awt.event.*;



public class phone
{
public void new_record()
{
String id,name,city,add,number,total,list;
boolean bln=false;
try
{
Properties pr=new Properties();
FileInputStream fin=new FileInputStream("phone.dat");
if(fin!=null)
{
pr.load(fin);
}
BufferedReader br1=new BufferedReader (new InputStreamReader(System.in));
FileOutputStream fout=new FileOutputStream("phone.dat");
for(;;)
{
System.out .println("Enter the 'ID', 'q' for quit:");
id=br1.readLine().toUpperCase();
bln=pr.containsKey(id);
System.out .println("FLag"+bln);
if(bln)
{
System.out.println("ID id already exists, Please Enter another ID:");
continue;
}
if((id.toUpperCase()).equals("Q"))
break;
System.out.println("enter name:");
name=br1.readLine().toUpperCase();
System.out.println("enter Phone number:");
number=br1.readLine().toUpperCase();
System.out.println("enter address:");
add=br1.readLine().toUpperCase();
System.out.println("enter city:");
city=br1.readLine().toUpperCase();
total=" Name="+name+","+"Phone no="+number+","+" Address="+add+","+" City="+city;
total=total.toUpperCase();
pr.put(id,total);
pr.store(fout,"My Telephone Book");
}
fout.close();
}
catch(Exception e)
{
System.out.println(e);
}
}

public void display_record()
{
String id="";
String total="";
int x=1;
try
{
FileInputStream fin=new FileInputStream("phone.dat");
Properties pr1=new Properties();
pr1.load(fin);
Enumeration enum1=pr1.keys();
while(enum1.hasMoreElements())
{
id=enum1.nextElement().toString();
total=pr1.get(id).toString();
StringTokenizer stk=new StringTokenizer(total,"=,");
System.out .println("RECORD ::"+x+"\n");
x++;
while(stk.hasMoreTokens())
{
String key=stk.nextToken();
String value=stk.nextToken();
System.out.println("\t"+key+"::\t\t::"+value);
try
{
Thread.sleep(1500);
}
catch(Exception e){}
}
System.out.println("");
System.out.println("");
}
fin.close();
}
catch(Exception e){}
}
public void display_by_name()
{
String name="",id,total;
String key[]=new String[4];
String value[]=new String[4];
int i=0;

System.out.println("Enter Name For Searching Record :-");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
name=br.readLine().toUpperCase();
FileInputStream fin=new FileInputStream("phone.dat");
Properties pr1=new Properties();
pr1.load(fin);
Enumeration enum1=pr1.keys();
while(enum1.hasMoreElements())
{
id=enum1.nextElement().toString();
total=pr1.get(id).toString();
StringTokenizer stk=new StringTokenizer(total,"=,");

while(stk.hasMoreTokens())
{

for(i=0;i<4;i++)
{
key[i]=stk.nextToken();
value[i]=stk.nextToken();
}
if(value[0].equals(name))
{
for(i=0;i<4;i++)
{
System.out.println("\t"+key[i]+":"+value[i]);
try
{
Thread.sleep(1500);
}
catch(Exception e){}
}


}
}
System.out.println("");


}
fin.close();
}
catch(Exception e){
System.out.println(e);
}
}

public void display_by_city()
{
String city="",id,total;
String key2[]=new String[4];
String value2[]=new String[4];
int i=0;

System.out.println("Enter City For Searching Record :-");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
city=br.readLine().toUpperCase();
FileInputStream fin=new FileInputStream("phone.dat");
Properties pr1=new Properties();
pr1.load(fin);
Enumeration enum1=pr1.keys();
while(enum1.hasMoreElements())
{
id=enum1.nextElement().toString();
total=pr1.get(id).toString();
StringTokenizer stk=new StringTokenizer(total,"=,");

while(stk.hasMoreTokens())
{
key2[i]=stk.nextToken();
value2[i]=stk.nextToken();
// System.out.println("aaaaaaaaaaaaaaa"+value2[i]);
if(i==3)
{
if(value2[i].equals(city))
{
for(int j=0;j<4;j++)
{
System.out.println("\t"+key2[j]+":\t"+value2[j]);
try
{
Thread.sleep(1500);
}
catch(Exception e){}

}
}
}
i++;
if(i>3)
i=0;
}
System.out.println("");
System.out.println("");
}
fin.close();
}
catch(Exception e){
System.out.println(e);
}


}

public void display_record_first_letter()
{

String name="",id,total,str="";
String key2[]=new String[4];
String[] value2=new String[4];
int i=0;


try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter The First Letter Of any Name:");
name=br.readLine();
name=name.substring(0,1).toUpperCase();
FileInputStream fin=new FileInputStream("phone.dat");
Properties pr1=new Properties();
pr1.load(fin);
Enumeration enum1=pr1.keys();
while(enum1.hasMoreElements())
{
id=enum1.nextElement().toString();
total=pr1.get(id).toString();
StringTokenizer stk=new StringTokenizer(total,"=,");

while(stk.hasMoreTokens())
{

for(i=0;i<4;i++)
{
key2[i]=stk.nextToken();
value2[i]=stk.nextToken();
}
str=value2[0].substring(0,1);

if(str.equals(name))
{
for(i=0;i<4;i++)
{
System.out.println("\t"+key2[i]+":\t"+value2[i]);
try
{
Thread.sleep(1500);
}
catch(Exception e){}
}
}

}
System.out.println("");
System.out.println("");



}
fin.close();
}
catch(Exception e){
System.out.println(e);
}
}
public void replace_record()
{
String id,name,city,add,number,total,list;
boolean bln=false;
try
{
Properties pr=new Properties();
FileInputStream fin=new FileInputStream("phone.dat");
if(fin!=null)
{
pr.load(fin);
}
BufferedReader br1=new BufferedReader (new InputStreamReader(System.in));
FileOutputStream fout=new FileOutputStream("phone.dat");
for(;;)
{
System.out .println("Enter the 'ID', 'q' for quit:");
id=br1.readLine().toUpperCase();
if((id.toUpperCase()).equals("Q"))
break;

bln=pr.containsKey(id);
if(bln)
{
System.out.println("ID id already exists, ");


System.out.println("enter name:");
name=br1.readLine().toUpperCase();
System.out.println("enter Phone number:");
number=br1.readLine().toUpperCase();
System.out.println("enter address:");
add=br1.readLine().toUpperCase();
System.out.println("enter city:");
city=br1.readLine().toUpperCase();
total=" Name="+name+","+"Phone no="+number+","+" Address="+add+","+" City="+city;
total=total.toUpperCase();
pr.put(id,total);
pr.store(fout,"My Telephone Book");
}
else
{
System.out.println("ID does'nt Exists, Please Enter A Valid ID:");
continue;
}

}
pr.store(fout,"My Phone Book");
fout.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
public void delete_record()
{

String id="";
boolean bln=false;
try
{
Properties pr1=new Properties();
FileInputStream fin=new FileInputStream("phone.dat");
if(fin!=null)
pr1.load(fin);

BufferedReader br1=new BufferedReader (new InputStreamReader(System.in));
FileOutputStream fout=new FileOutputStream("phone.dat");
for(;;)
{
System.out .println("Enter the 'ID', 'q' for quit:");
id=br1.readLine().toUpperCase();
if((id.toUpperCase()).equals("Q"))


break;

bln=pr1.containsKey(id);

if(bln)
{
System.out.println("ID exists :");
String str=pr1.remove(id).toString();
pr1.store(fout,"My Phone Book");
try
{
Thread.sleep(1500);
}
catch(Exception e){}
System.out.println("Record deleted successfully");
}
else
{
System.out.println("Enter Existing ID:");
pr1.store(fout,"My Phone Book");
}

}
pr1.store(fout,"My Phone Book");
fin.close();
fout.close();
}
catch(Exception e)
{
System.out.println(e);
}
}




public void menu()
{
char ch=30;
char ch1=31;
int l;
for(int i=0;i<27;i++)
{
System.out.print(" ");
}
for(l=0;l<2;l++)
{

for(int j=0;j<38;j++)
{

System.out.print(ch);
}
System.out.println("");
for(int k=0;k<27;k++)
{
System.out.print(" ");
}
}
System.out.print(ch);
System.out.print(ch1);
for(int i=0;i<34;i++)
System.out.print(" ");
System.out.print(ch);
System.out.print(ch1);
System.out.println("");
for(int i=0;i<27;i++)
System.out.print(" ");

System.out.print(ch);
System.out.print(ch1+" ");
System.out.print (" 1. Enter new Record: ");

System.out.print(" "+ch);
System.out.println(ch1+" ");

for(int i=0;i<26;i++)
System.out.print(" ");
System.out.print(" "+ch1);
System.out.print(ch+" ");


System.out.print (" 2. Display All Record: ");
System.out.print(" "+ch);
System.out.println(ch1+" ");


for(int i=0;i<26;i++)
System.out.print(" ");
System.out.print(" "+ch);
System.out.print(ch1+" ");


System.out.print (" 3. Search Record by name: ");
System.out.print(" "+ch);
System.out.println(ch1+" ");


for(int i=0;i<26;i++)
System.out.print(" ");
System.out.print(" "+ch);
System.out.print(ch1+" ");

System.out.print (" 4. Search Record by city: ");
System.out.print(" "+ch);
System.out.println(ch1+" ");


for(int i=0;i<26;i++)
System.out.print(" ");
System.out.print(" "+ch);
System.out.print(ch1+" ");


System.out.print (" 5. Search Record by 1st letter:");
System.out.print(" "+ch);
System.out.println(ch1+" ");


for(int i=0;i<26;i++)
System.out.print(" ");
System.out.print(" "+ch);
System.out.print(ch1+" ");


System.out .print(" 6. Replace Record: ");
System.out.print(" "+ch);
System.out.println(ch1+" ");


for(int i=0;i<26;i++)
System.out.print(" ");
System.out.print(" "+ch);
System.out.print(ch1+" ");


System.out .print(" 7. Delete Record: ");
System.out.print(" "+ch);
System.out.println(ch1+" ");


for(int i=0;i<26;i++)
System.out.print(" ");
System.out.print(" "+ch);
System.out.print(ch1+" ");


System.out .print(" 8. Exit: ");
System.out.print(" "+ch);
System.out.println(ch1+" ");


for(int j=0;j<27;j++)
System.out.print(" ");
System.out.print(ch);
System.out.print(ch1);
for(int i=0;i<34;i++)
System.out.print(" ");
System.out.print(ch);
System.out.print(ch1);
System.out.println("");
for(int i=0;i<27;i++)
System.out.print(" ");
for(int i=0;i<38;i++)
System.out.print(ch);
System.out.println("");
for(int i=0;i<27;i++)
System.out.print(" ");
for(int i=0;i<38;i++)
System.out.print(ch);






}




public static void main (String[] args)
{
phone cl=new phone();
BufferedReader br;


for(;;)
{
try
{
cl.menu();
br=new BufferedReader (new InputStreamReader(System.in));
System.out.print(" Enter Your Choice :");
String choice=br.readLine();
if(choice.equals ("1"))
{
cl.new_record();
}
else if(choice.equals("2"))
{
cl.display_record();
}
else if(choice.equals("3"))
cl.display_by_name();
else if(choice.equals("4"))
{
cl.display_by_city();
}
else if(choice.equals("5"))
cl.display_record_first_letter();
else if(choice.equals("6"))
cl.replace_record();
else if(choice.equals("7"))
cl.delete_record();

else if(choice.equals("8"))
System.exit(0);
}
catch(Exception e){}
}


}
}


Notice from BuffaloHELP:
You must use HTML or CODEBOX (for long codes like this) for all html codes. You should not be receiving hosting credits for codes entered.

 

 

 


Reply

sportytalk
I don't really have a need for using a java programmed address book at the moment, but if I ever do need to use an address book or something similar, I will use this tutorial to get an address book up and running.

The code looks like the program's good, but it needs compiling before it becomes of any use. Have you got any examples of this application or somewhere where you can download the compiled file of this program?

Thanks for sharing this anyway, it'll be extremely useful to those who prefer to keep an address book of friends/relatives on their computers smile.gif


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : building address java

  1. How To Disable Firefox's Awesome Bar - restore old location/address bar from firefox 2 (7)
  2. Verifying Your Email Address - A Simple PHP Tutorial. (0)
    Hi, everyone. I'm offering the next PHP tutorial that verifies an email address of an user who
    wants to register on your site. This is how it works. When someone, went to registration page and
    filled out all of the details such as Name, Password, email and so on and submit those details to a
    server. The user is getting unique registration code to his or her email account, the one has to be
    clicked to activate the account. 1. Outline: a) You need to create or build the following files:
    -config.php (The configuration file) -signup.php (Initial Registration) ...
  3. Making a java based program - (3)
    Java GUI Making a Little Java Program Sec. 1: Imports and starting it off Sec. 2: Variables Sec.
    3: Frame and Stuff Sec. 4: Declaring buttons Sec. 5: Adding buttons Sec. 6: Action Listening Sec. 7:
    Using this for a learning experience Section 1 Now, let's think. What imports do we need? We
    obviously need GUI imports. We also need the action Listener. So, let's declare this at the very
    top: Code: CODE import java.awt.*; import java.awt.event.*; import javax.swing.*; That's
    all we need to get all our supplies. Now to start us off. Skip a couple lines ...
  4. Templating System Using Php Includes - Building a Dynamic site using Includes and flat-files. (13)
    Php based Templating System http://jlhaslip.trap17.com/template/index.php The Source Code
    for the scripts are included (literally) on the different pages on the Demo, including the Contact /
    Email script. The only page not there yet is the Message Script. Maybe tonight I will upload that.
    This one uses a little bit of query-string checking to confirm that the contents of the page are
    actually available (file_exists())and an allowed page content before serving it up. The
    'allowed page content' is done by checking against a flat-file containing an array...
  5. Building An Unordered List Of Anchors - From a list of files in a folder (4)
    Building an Unordered List of Anchors from a Folder of files This script reads files from the
    named folder and then builds a list of links from the filenames using the contents of the first
    tag as the link description and the file-name as the anchor href . The file should be a web
    readable file such as html or php. This script may come in handy for creating a list of clickable
    links for use in sidebars and on 'sitemap' pages. Simply build a series of html files and
    store them all in a single folder, ensuring that the description inside the first h3...
  6. Configuring Dns Settings For Website - Nameserver, A-Address, DNS configuration (1)
    Most of the people have problems configuring their website. After registering their domain, many get
    confused in configuring their domain (basically the DNS settings). This small tutorial will help you
    get started with DNS settings configuration. A domain can be basically pointed to a server using 2
    main techniques. 1. Nameserver (the most recommended way) 2. A Address (Not recommended)
    Configuring using Nameservers ==================== Login to your domain Control panel provided by
    your DOMAIN NAME PROVIDER / REGISTRAR. Not your webhost. Search for settings like DN...
  7. An Introduction To Java And Graphics - (5)
    Table of Contents I. Introduction II. Before You Begin III. Necessities in a Java Program IV.
    Creating a Canvas V. Shapes A. Line B. Rectangle C. Oval D. Polygon VI. Other Things A.
    Changing the Color B. Strings 1. Changing the Font 2. Drawing a String C. Images VII.
    Conclusion I. Introduction Welcome to my second tutorial here at Trap17. I'm going on
    vacation for a week so I thought I'd leave you all with some of the things that I picked up in
    the class I took ealier this summer. If anyone wants to see some things that I've done, t...
  8. Jalbum 5.2 - Gallery Building Software (0)
    Just thought I would create a tutorial for those who want to use a gallery on their site but
    don't know how. This tutorial looks at JAlbum 4.4.2, which you can get by going here GET JAlbum
    5.2 // Step 1 JAlbum allows you to create a photo album without even leaving its main page. Here
    you can select the image directory, output directory, common album settings and control the album
    generation. // Step 2 To select an image directory, just drag and drop an image folder onto the
    Image Directory bar. JAlbum can read photos in JPG, GIF and PNG formats as well as movie ...
  9. Showing A Users Ip Address In Php - (5)
    In this simple tutorial I will explain how to show a users IP address in your website statistics.
    Open the file you would like to show the IP in, and insert this code where you want it to show:
    CODE <?php echo $_SERVER["REMOTE_ADDR"]; ?> An IP Address
    whould show up. If you would like to have text infront of the IP, for example, IP Address: Blah
    Blah. Type: CODE IP Address: <?php echo $_SERVER["REMOTE_ADDR"];
    ?> Make sure to save the file as '.php' format, otherwise the code will...



Looking for building, address, book, java

Searching Video's for building, address, book, java
advertisement



Building An Address Book With Java



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE