Welcome Guest ( Log In | Register )




                Web Hosting Guide

 
Reply to this topicNew Topic
Java Phone Book, A console base java phone book program
vicky99
post May 28 2006, 10:17 AM
Post #1


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 54
Joined: 28-May 06
Member No.: 13,691


Hello everybody
I wrote this program a few months back when I wan learning JAVA.I haven't learned java yet though because its to vast.
The program is console based. It uses the util package of java in a great way. It keeps record in a flat file. You should create a Data file called phone.dat in same folder where phone.java is kept.
So here is the code:-(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(" &

This post has been edited by vicky99: May 28 2006, 10:32 AM
Go to the top of the page
 
+Quote Post
iGuest
post Oct 2 2007, 07:32 PM
Post #2


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869


I need a console based application but the problem is half of the code is missing from the main method.

-Rageeb
Go to the top of the page
 
+Quote Post
iGuest
post Jan 21 2008, 02:19 AM
Post #3


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869


Thanks my friend GOdBless�
Java Phone Book

Thank you very much!..
GODBLESS�

-jonathan
Go to the top of the page
 
+Quote Post
iGuest
post Aug 26 2009, 04:58 AM
Post #4


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869


is it working?
Java Phone Book

I grab your program but when I run it it has a lot of errors 

-reply by jubal garlejo
Go to the top of the page
 
+Quote Post
iGuest
post Oct 1 2009, 04:54 PM
Post #5


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869


To (G)jubal garlejo
Java Phone Book

you need to add imports to first line of your code ;

import java .Io .*;
Import java .Util .*;
Import java.Awt.Event.*;
 

-reply by Ramiz


Go to the top of the page
 
+Quote Post
iGuest
post Oct 6 2009, 07:02 AM
Post #6


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869


Catch or Try and Finally
Java Phone Book

Hi I'm a computer programming student and I want to study your work so that I can create my own but then when I copied the program,, and created the DAT file as you instructed, I have a problem on the bottom part of the program in which the command TRY is underlined with red and it says there, try without finally or catch. I tried using finally or catch but I cannot debug it or fix it. Please tell me more what to do with it because I really need to learn programming. I know the concepts used in it but the program doesn't work at all. Do I need to create other files etc,,

 

Please reply me on my email because I may forgot this site.

THANK YOU VERY MUCH.

Daniel Clint M. Soliba

dsoliba@yahoo.Com

-reply by Daniel

 

Go to the top of the page
 
+Quote Post

Reply to this topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   1 lemen 34 17th November 2009 - 07:54 AM
Last post by: MJboy
No New Posts   1 lemen 74 6th November 2009 - 08:30 AM
Last post by: MJboy
No New Posts 11 divinity 1,584 4th November 2009 - 12:35 AM
Last post by: HannahI
No New Posts   7 amit nigam 1,233 4th November 2009 - 12:33 AM
Last post by: HannahI
No New Posts   5 sparkx 704 4th November 2009 - 12:29 AM
Last post by: HannahI
No New Posts   17 tommy_vercetti 7,374 1st November 2009 - 05:08 PM
Last post by: iG-ross
No New Posts   7 kaaamooo 920 30th October 2009 - 01:21 PM
Last post by: Quatrux
No New Posts   6 FearfullyMade 2,894 28th October 2009 - 08:14 PM
Last post by: HannahI
No New Posts   11 www.jbi.in 473 23rd October 2009 - 10:35 PM
Last post by: yordan
No New Posts   11 cyph3r 6,034 22nd October 2009 - 05:33 PM
Last post by: iG-Atom
No New Posts   17 vicky99 6,429 17th October 2009 - 11:51 AM
Last post by: surfermac
No New Posts   0 Entheone 50 28th September 2009 - 10:15 AM
Last post by: Entheone
No New Posts   0 starscream 130 24th September 2009 - 12:14 PM
Last post by: starscream
No New Posts   12 tansqrx 1,413 15th September 2009 - 11:04 AM
Last post by: iG-Rich
No New Posts   7 suicide 5,152 5th September 2009 - 02:11 PM
Last post by: iG-sam


Web Hosting Powered by ComputingHost.com.