Create a java program that will use the attached input file and perform the foll
ID: 3711767 • Letter: C
Question
Create a java program that will use the attached input file and perform the following operations. Read the file into an appropriate JCF data structure. Look up a (list of) names and numbers matching a last name or the first letters of a last name, ignoring case. Look up a (list of) names and numbers matching a number or the first digits of a number. Add a name and number to the list. Sort the list by first name, last name or phone number and display it, one page (10 numbers) at a time ("press enter to continue"). Save the list to the disk. Write a driver to display a menu of the options listed above and repeat actions until the user selects an Exit option.
Explanation / Answer
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
class Demo
{
String lookup=null;
Reader r=null;
public void lookupByLastName(Scanner sc)
{
try
{
System.out.println("Enter last name or the first letters of a last name:");
lookup=sc.next();
File f = new File("file.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
while ((readLine = b.readLine()) != null)
{
if(readLine.contains(lookup))
{
System.out.println(readLine);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void lookupByNumber(Scanner sc)
{
try
{
System.out.println("Enter number or the first digits of a number:");
lookup=sc.next();
File f = new File("file.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
while ((readLine = b.readLine()) != null)
{
if(readLine.contains(lookup))
{
System.out.println(readLine);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void addNameNumber(Scanner sc)
{
try
{
File file =new File("file.txt");
if(!file.exists())
{
file.createNewFile();
}
FileWriter fw = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println("");
System.out.println("Enter number of records that you want to insert into file:");
int noRecord=sc.nextInt();
String record=null;
for (int i=0;i<noRecord ;i++ )
{
System.out.println("Enter FirstName,LastName,ContactNumber format:");
record=sc.next();
pw.println(record);
record=null;
}
pw.close();
System.out.println("Data successfully appended at the end of file");
}
catch(IOException ioe)
{
System.out.println("Exception occurred:");
ioe.printStackTrace();
}
}
}
class ReadWrite
{
public static void main(String[] args)
{
Demo d1=new Demo();
int choice=0;
Scanner sc=new Scanner(System.in);
while(choice!=4)
{
System.out.println("Select you choice from following... 1.Look up a (list of) names and numbers matching a last name or the first letters of a last name. 2.Look up a (list of) names and numbers matching a number or the first digits of a number. 3. Add a name and number to the list. Sort the list by first name, last name or phone number and display it, one page (10 numbers) at a time. 4.Exit");
choice=sc.nextInt();
switch(choice)
{
case 1:
d1.lookupByLastName(sc);
break;
case 2:
d1.lookupByNumber(sc);
break;
case 3:
d1.addNameNumber(sc);
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.