Question: How do I make a method that will make it so the list of contacts in my program are sorted by their phone numbers? Here's
How do I make a method that will make it so the list of contacts in my program are sorted by their phone numbers?
Here's my code so far:
import java.io.*;
import java.util.*;
class Entry {
public String name, number, note;
}
public class PBN {
public static Entry[] contactList;
public static int num_entries;
public static Scanner stdin = new Scanner(System.in);
public static void main(String args[]) throws Exception{
int i; char C;
String code, Command;
contactList = new Entry[200];
num_entries = 0;
try {
readPhoneBook("phonebook.txt");
} catch (FileNotFoundException e) {
}
System.out.println("Codes are entered as 1 to 8 characters. Use" +
" \"e\" for enter," +
" \"f\" for find," +
" \"l\" for listing all the entries," +
" \"q\" to quit.");
Command = null;
C = ' ';
while(C != 'q'){
System.out.print("Command: ");
Command = stdin.next();
C = Command.charAt(0);
switch (C) {
case 'e': addContact(); break;
case 'f':
code = stdin.next();
stdin.nextLine();
i = index(code);
if (i >= 0) displayContact(contactList[i]);
else System.out.println("**No entry with code " + code); break;
case 'l':
listAllContacts(); break;
case 'q':
CopyPhoneBookToFile("PhoneBook1.txt");
System.out.println("Quitting the application. All the entries are "
+ "stored in the file PhoneBook1.txt"); break;
case 'a':
sortList(); break;
case 'm':
default:
System.out.println("Invalid command Please enter the command again!!!");
}
}
}
public static void readPhoneBook(String FileName) throws Exception {
File F;
F = new File(FileName);
Scanner S = new Scanner(F);
while (S.hasNextLine()) {
contactList[num_entries]= new Entry();
contactList[num_entries].name = S.next();
contactList[num_entries].number = S.next();
contactList[num_entries].note = S.nextLine();
num_entries++;
}
S.close();
}
public static void addContact() {
String name = stdin.next();
String number;
stdin.nextLine();
String pattern = "^\\(?(\\d{3})?\\)?[- ]?(\\d{3})[- ](\\d{4})$";
contactList[num_entries] = new Entry();
contactList[num_entries].name = name;
System.out.print("Enter Number: ");
number = stdin.nextLine();
if (number.matches(pattern)) {
contactList[num_entries].number = number;
} else {
System.out.println("Error!");
System.out.println("Not proper digit format! Use \"012-3456\", \"(012)345-6789\"" +
", or \"012-345-6789\" format.");
System.out.print("Enter number: ");
number = stdin.nextLine();
}
System.out.print("Enter Notes: ");
contactList[num_entries].note = stdin.nextLine();
num_entries++;
System.out.println(" ");
}
public static int index(String Key) {
// Function to get the index of a key from an array
// if not found, returns -1
for (int i=0; i < num_entries; i++) {
if (contactList[i].name.equalsIgnoreCase(Key))
return i; // Found the Key, return index.
}
return -1;
}
public static void displayContact(Entry contact) {
System.out.println("--"+ contact.name+"\t");
System.out.println("--"+ contact.number+"\t");
System.out.println("--"+ contact.note);
System.out.println("");
}
public static void listAllContacts() {
int i = 0;
while (i < num_entries) {
displayContact(contactList[i]);
i++;
}
}
public static void sortList() {
int i;
Entry temp;
temp = new Entry();
for (int j = 0; j< num_entries; j++) {
for (i = j + 1; i < num_entries; i++) {
if (contactList[j].name.compareToIgnoreCase(contactList[i].name)> 0) {
temp = contactList[j];
contactList[j] = contactList[i];
contactList[i] = temp;
}
}
}
}
public static void CopyPhoneBookToFile(String FileName) throws Exception{
FileOutputStream out = new FileOutputStream(FileName);
PrintStream P = new PrintStream( out );
for (int i=0; i < num_entries; i++) {
P.println(contactList[i].name + "\t" + contactList[i].number +"\t" + contactList[i].note);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
