Question: HELP IN JAVA: Write a telephone lookup program. Read a data set of 1000 names and telephone numbers from files that contain contact information. Your
HELP IN JAVA:
Write a telephone lookup program. Read a data set of 1000 names and telephone numbers from files that contain contact information. Your program has to work like a caller ID system, where given a number it displays the name. (There will be no duplicate names or numbers.) Use selection sort for sorting. Use a binary search for lookup.
The program has to do two things: read in the data and handle user queries.
1. The data is in two files. For each person, the name is listed in one file and the phone number is listed in the other. The first phone number belongs to the first person, the second number to the second person etc. Create a Contact class (name and phone number are the only attributes), make Contact objects and put them in an array.
2. The program should prompt the user with this message: Please enter a phone number, or * to quit. The user enters a phone number, and the program prints the name and the phone number (separated by a space) for the corresponding contact. If a match cannot be found, the program should print Unknown number. Repeat this until the user enters *.
This is what I have done:
PhoneLookup.java:
public class PhoneLookup { public static void selectionSort (Contact[]A) { for(int i=0; i
file1Input = new Scanner(File1);
}
catch (FileNotFoundException e) {
} File File2 = new File("phonenumbers.txt"); Scanner file2Input = null; //try and catch try {
file2Input = new Scanner(File2);
}
catch (FileNotFoundException e) {
} Contact[] D=new Contact[1000]; for (int i = 0; i<1000; i++) { String name = file1Input.nextLine(); String phoneNum = file2Input.nextLine(); D[i] = new Contact(name, phoneNum); } selectionSort(D); Scanner s = new Scanner(System.in); String newNumber; while (true){ System.out.println("Input phone number to lookup: "); newNumber = s.next(); if (newNumber.equals("*")){ break; } else { int results = binarySearch(D, newNumber); System.out.println("Results: "+results); } } } }
Contact.java:
public class Contact implements Comparable
public void setName(String name) { this.name = name; }
public String getPhoneNumber() { return phoneNumber; }
public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String toString() { return name + " " + phoneNumber; }
public int compareTo(Contact tt){ if(this.name.compareTo(tt.name)>0) return 1; else if(this.name.compareTo(tt.name)<0) return -1; else return 0; } public int compareTo(String rr){ if(this.name.compareTo(rr)>0) return 1; else if(this.name.compareTo(rr)<0) return -1; else return 0; } }
phonenumbers.txt:
486-685-8308 711-493-6467 871-805-7191 914-344-2341 972-724-5804 688-676-8778 533-157-4369 773-441-5964 982-299-7011 738-457-5223
names.txt:
Eduardo Ashley Aryana Foley Emmy Richmond Alfredo Meadows Chaz Bailey Kael Hurst Jayden Ho Alissa Armstrong Andre Warner Emelia Duffy
I'm having trouble figuring out how to sort both names alphabethically and phonenumbers least to greatest. Also having trouble with this part "If a match cannot be found, the program should print Unknown number. Repeat this until the user enters *." Lastley having trouble printing out the result of the number's corresponfing name.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
