i was trying to do this program but i was having problems getting the binary search and merge sort to work i dont understand it that much and i was wondering if anyone could set me on the right path.. aight the purpose is to read data from an input file of a set of names and telephone numbers. the program is supposed to hve lookups by name and also by phone number and its supposed to use binary search and merge sort... now most of working so that it reads the name and numbers from the input file and prints what is looked up but i didnt get the binary search and merge sort...just looking for a lil help to understand this..
this is the telephone lookup class that reads the input...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileNotFoundException;
publicclass TelephoneLookup
{
publicstaticvoid main(String[] args)
{ // read data from a file
FileReader reader = null;
// prompt user for filename
ConsoleReader console = new ConsoleReader(System.in);
System.out.print("Enter the name of the phonebook file: ");
String fileName = console.readLine();
LookupTable names = new LookupTable(1000);
LookupTable numbers = new LookupTable(1000);
//open the input file
try
{ reader = new FileReader(fileName);
names.read(new BufferedReader(reader));
numbers.read(new BufferedReader(reader));
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find input file ");
System.exit(1);
}
catch (IOException e)
{ System.out.println("Cannot open input file ");
System.exit(2);
}
boolean more = true;
while (more)
{ System.out.println("Lookup N)ame, P)hone number, Q)uit?");
String cmd = console.readLine().toUpperCase();
if (cmd.equals("Q")) more = false;
elseif (cmd.equals("N"))
{ System.out.println("Enter name:");
String n = console.readLine();
//call the lookup method in the LookupTable class
System.out.println("Phone number: " + names.lookup(n));
}
elseif (cmd.equals("P"))
{ System.out.println("Enter phone number:");
String n = console.readLine();
//call the lookup method in the LookupTable class
System.out.println("Name: " + numbers.lookup(n));
}
}
}
}
this is the lookup class that is supposed to lookup whichever the user chooses...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileNotFoundException;
publicclass LookupTable
{
private Item[] items;
int itemCount;
public LookupTable(int n)
{ items = new Item[n];
itemCount = 0;
}
publicvoid read(BufferedReader in) throws IOException
{ boolean more = true;
while (more && itemCount < items.length)
{ String k = in.readLine();
String v = null;
if (k != null) v = in.readLine();
if (v != null)
{ items[itemCount] = new Item(k, v);
itemCount++;
}
else more = false;
}
// must sort before you can call binarySearch
mergeSort(items, 0, itemCount - 1);
}
// THERE IS CODE IN HERE YOU MUST ADD TO MAKE THIS WORK; PERHAPS YOU WANT TWO DIFFERENT
// lookup METHODS??
public String lookup(String k)
{ int pos = binarySearch(items, 0, itemCount - 1, k);
if (pos < 0)
return "Not found";
elsereturn items[pos].getValue();
}
publicstaticvoid merge(Item[] a, int from, int mid, int to)
{ //YOU WRITE THIS CODE
}
publicstaticvoid mergeSort(Item[] a, int from, int to)
{ //YOU WRITE THIS CODE
}
publicstaticint binarySearch(Item[] v, int from, int to, String a)
{ //YOU WRITE THIS CODE
}
}
this is the item class that handles the keys chosen