Space for google add

Write a menu driven program to perform the following operations on a text file “phone.txt”.

 


Write a menu driven program to perform the following operations on a text file “phone.txt”

which contains name and phone number pairs. The menu should have options:

i. Search name and display phone number

ii. Add a new name-phone number pair.


Ex5seta2.java

import java.io.*;

import java.util.*;


class phone

{

public static void main(String args[]){

try

{

int ch;

String[] part=null;

String name,contact,line=null,tname;

do{

int flag=0;

Scanner sc=new Scanner(System.in);


BufferedReader fr=new BufferedReader(new FileReader("phone.txt"));

System.out.println("1:Search name and display contact\n2:Add new contact\n3:Exit");

System.out.println("Enter your choice");

ch=sc.nextInt();

switch(ch)

{

case 1:System.out.println("Enter the name to be searched :");

tname=sc.next();

while((line=fr.readLine())!=null)

{

part=line.split(" ");

         if(part[0].equals(tname)){

   flag=1;

   break;

}

}

if(flag==1)

   System.out.println(part[0]+" "+part[1]);

else

   System.out.println("Name is not in list");

fr.close();

break;

case 2:System.out.println("Enter name and contact :");

name=sc.next();

contact=sc.next();

FileWriter fw=new FileWriter("phone.txt",true);

fw.write(name+" "+contact+"\n");

fw.close();

break;

case 3:break;

default:System.out.println("Invalid Choice");

}

}while(ch!=3);

}catch(Exception e){

System.out.println(e);

}

}

}


Post a Comment

0 Comments