Java Program to perform operations on phone.txt text file - IProgramX

Q. 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. 

Program:

import java.io.*;
import java.util.*;
class phone
{
                String name,phno;
                phone (String nm,String ph)
                {
                                name = nm;
                                phno=ph;
                }
                public String toString ()
                {
                                String s = name + " " + phno ;
                                return (s);
                }
                public static void search(phone[] arr,int n)throws IOException
                {
                                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                String s=br.readLine();
                                for(int i=0;i<n;i++)
                                {
                                                if(arr[i].name.equals(s))
                                                {
                                                                System.out.println(arr[i].toString());
                                                                return;
                                                }
                                }
                                System.out.println("No Records Found");           
                }
}
class Slip14_1
{
                public static void main (String args[]) throws IOException
                {
                                String s, space = " ";
                                int i;
                                File f = new File ("phone.dat");
                                RandomAccessFile rf = new RandomAccessFile (f, "rw");
                                BufferedReader b = new BufferedReader (new InputStreamReader (System.in));
                                System.out.println ("Enter no.of Customer");
                                int ch,n;
                                n = Integer.parseInt (b.readLine ());
                                System.out.println ("Enter Records:\n <name><phone no> :");
                                for (i = 0; i < n; i++)
                                {
                                                s = b.readLine () + "\n";
                                                rf.writeBytes(s);
                                }
                                rf.close ();
                                RandomAccessFile rf1 = new RandomAccessFile (f, "r");
                                phone p[] = new phone[n];
                                for (i = 0; i < n; i++)
                                {
                                                s = rf1.readLine ();
                                                StringTokenizer t = new StringTokenizer (s, space);
                                                String sn = new String (t.nextToken ());
                                                String sp = new String (t.nextToken ());
                                                p[i] = new phone(sn,sp);
                                }
                                do
                                {
                                                System.out.println("Menu :\n"+"1:Search for a phone no by name\n"+"2:Add a new Record \n"+"3:Exit\n"+"Enter your Choice :" );
                                                ch=Integer.parseInt(b.readLine());
                                                switch (ch)
                                                {                             
                                                                case 1:
                                                                                System.out.println ("Enter name to be searched");
                                                                                phone.search (p,n);
                                                                                break;
                                                                case 2:
                                                                                rf = new RandomAccessFile (f, "rw");
                                                                                System.out.println ("Enter Records:\n <name><phone no> :");
                                                                                String s1 = b.readLine () + "\n";
                                                                                rf.writeBytes(s1);
                                                                                rf.close();
                                                                                rf1 = new RandomAccessFile (f, "r");
                                                                                s = rf1.readLine ();
                                                                                StringTokenizer t = new StringTokenizer (s, space);
                                                                                String sn = new String (t.nextToken ());
                                                                                String sp = new String (t.nextToken ());
                                                                                phone p1 = new phone(sn,sp);
                                                                                System.out.println(" Records are  ");
                                                                                for(i=0;i<n;i++)
                                                                                                System.out.println(p[i].toString());
                                                                                System.out.println(p1.toString());
                                                                                break;
                                                                case 3 :System.exit(0); 
                                                }
                                }while(ch!=3);
                }
}

Post a Comment

1 Comments

  1. Slip14_2: Write a Java program to create a Package “SY” which has a class SYMarks (members –
    ComputerTotal, MathsTotal, and ElectronicsTotal). Create another package TY which has a class
    TYMarks (members – Theory, Practicals). Create ‘n’ objects of Student class (having rollNumber,
    name, SYMarks and TYMarks). Add the marks of SY and TY computer subjects and calculate the
    Grade (‘A’ for >= 70, ‘B’ for >= 60 ‘C’ for >= 50, Pass Class for > =40 else‘FAIL’) and display the result
    of the student in proper format

    Package
    package SY;
    public class SYMarks
    {
    int ct,mt,et;
    public SYMarks(int ct,int mt,int et)
    {
    this.ct=ct;
    this.mt=mt;
    this.et=et;
    }
    public void display()
    {
    System.out.println("\nMarks are;");
    System.out.println("Computer\tMaths\tElectronics");
    System.out.println(ct+"\t"+mt+"\t"+et);
    }
    }
    Package 2
    package TY;
    public class TYMarks
    {
    int Theory,Practicals;
    public TYMarks(int Theory,int Practicals)
    {
    this.Theory=Theory;
    this.Practicals=Practicals;
    }
    public void display()
    {
    System.out.println("\nMarks are:");
    System.out.println("Theory\tPracticals");
    System.out.println(Theory+"\t"+Practicals);
    }
    }
    Mainfile
    import SY.SYMarks;
    import TY.TYMarks;
    import java.io.*;
    class SYTY
    {
    int rollno;
    int ComputerTotal, MathsTotal, ElecTotal, Theory, Practicals;
    String name;
    BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
    public SYTY()
    {}
    public SYTY(int rollno,String name) throws Exception
    {
    this.rollno = rollno;
    this.name = name;
    System.out.println("Enter SY marks: ");
    System.out.println("\nEnter computer marks");
    ComputerTotal = Integer.parseInt(br.readLine());
    while((ComputerTotal<0 || ComputerTotal>100))
    {
    System.out.println("\n\tInvalid marks.....");
    System.out.println("Please ReEnter the marks: ");
    ComputerTotal = Integer.parseInt(br.readLine());

    }
    System.out.println("\nEnter maths marks");
    MathsTotal=Integer.parseInt(br.readLine());
    while((MathsTotal<0 || MathsTotal>100))
    {
    System.out.println("\n\tInvalid marks.....");
    System.out.println("Please Reenter the marks: ");
    MathsTotal=Integer.parseInt(br.readLine());
    }
    System.out.println("\nEnter electronics marks");
    ElecTotal = Integer.parseInt(br.readLine());
    while((ElecTotal<0 || ElecTotal>100))
    {
    System.out.println("\n\tInvalid marks.....");
    System.out.println("Please Reenter the marks: ");
    ElecTotal = Integer.parseInt(br.readLine());
    }
    SYMarks sy = new SYMarks(ComputerTotal, MathsTotal, ElecTotal);
    System.out.print("\nEnter TY marks: ");
    System.out.print("\nEnter theory marks ");
    Theory = Integer.parseInt(br.readLine());
    while((Theory<0 || Theory>100))
    {
    System.out.println("\n\tInvalid marks.....");
    System.out.println("Please Reenter the marks: ");
    Theory = Integer.parseInt(br.readLine());
    }
    System.out.print("\nEnter practicals marks ");
    Practicals = Integer.parseInt(br.readLine());
    while((Practicals<0 || Practicals>100))
    {
    System.out.println("\n\tInvalid marks.....");
    System.out.println("Please Reenter the marks: ");
    Practicals = Integer.parseInt(br.readLine());
    }
    TYMarks ty = new TYMarks(Theory, Practicals);
    CalculateGrade();
    }
    public void getdata() throws Exception
    {
    System.out.println("\nEnter number of students: ");

    int n=Integer.parseInt(br.readLine());
    SYTY object[]=new SYTY[n];
    for(int i=0; i= 70)
    System.out.println("Grade:A");
    else if(percentage >= 60)
    System.out.println("Grade:B");
    else if(percentage >= 50)
    System.out.println("Grade:C");
    else if(percentage >= 40)
    System.out.println("Grade:PASS");
    else
    System.out.println("Grade:FAIL\n\tTry Again..........");
    }
    public static void main(String args[]) throws Exception
    {
    SYTY st = new SYTY();
    st.getdata();
    }
    }

    ReplyDelete