Friday 25 October 2013

Search From a list of telephone Numbers

/* A file contains a list of names and telephone numbers in the following form:-
AJIT                                        281050
ANIL                                       462890
RISHIKESH                            524352

The names contain only one word and the names and telephone numbers are separated by
white spaces. Write a interactive program to   :-

    Create the above file
    Display the contents of two columns
    Search a telephone number for a given name( Assure no duplication of name)
    Exit the program.


The program must create the program for the first time.
Test your program for the following data:-

AJIT                                        281050
ANIL                                       462890
RISHIKESH                            524352
JAVAGAL                              345678
MONGIA                                234561
RAHUL                                   765433
ROBIN                                    543221
SACHIN                                  765433
SAURAV                                8765908
VENKATESH                         7654322

Answer 3*/
import java.io.*;
class Names_Telephones
{
    public static void main(String args[])
    throws IOException
    {
        String filename="Directory.Txt";
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
        int i,n;
        String search;
      
        System.out.println("How many students");
        n=Integer.parseInt(br.readLine());
        String name,phone;
      
        FileWriter fw=new FileWriter(filename);
        BufferedWriter bw=new BufferedWriter(fw);
        PrintWriter outfile=new PrintWriter(bw);
      
        System.out.println("Enter "+n+" records one by one");
      
        //Writing Records into file
        for(i=1;i<=n;i++)
        {
            System.out.println("Name::");
            name=br.readLine();
            outfile.println(name);
          
            System.out.println("Phone Number::");
            phone=br.readLine();
            outfile.println(phone);
        }
        outfile.close();
      
        //Reading Records into file
        FileReader readfile=new FileReader(filename);
        BufferedReader infile=new BufferedReader(readfile);
      
        System.out.println("Enter name to search");
        search=br.readLine();
      
        while((name=infile.readLine())!=null)
        {
            phone=infile.readLine();
            if(name.compareToIgnoreCase(search)==0)
            {
                System.out.println("Name ::"+name+ " Phone number :: "+phone);
            }
        }
      infile.close();
    }

}

No comments:

Post a Comment