以下代码应该生成一个程序,允许用户搜索地址簿中的人,但是我在标记的以下代码行中遇到了一些问题。我不认为我错过了一个班级...这很奇怪。
我在java中相当新,所以如果这是基本的话我会道歉。
显然BinarySearchTree无法解析为类型
import java.util.Scanner;
import java.io.*;
public class useBinarySearchTree
{
public static void main (String[] args)
{
BinarySearchTee<AddrEntry> addrbook = new BinarySearchTree<AddrEntry>();// error exists here
AddrEntry inentry, tentry;
String nameLook;
Scanner keybd = new Scanner(System.in);
String again;
final int INORDER = 1;
int numentries;
int i;
int ok;
// create address book from input file
buildAddrBook(addrbook);
do
{
// prompt user for name to look up
System.out.println("Address/Phone Lookup:\n" + "Enter name: ");
nameLook = keybd.nextLine();
inentry = new AddrEntry(nameLook, null, null, null);
tentry = addrbook.get(inentry);
if (tentry != null)
{
// print the address book entry
System.out.println("\nAddress Book Entry\n");
System.out.println("__________________\n");
System.out.println(tentry);
System.out.println();
}
else
{
// if the person is not in the book, see if they should be added
System.out.println("\n" + nameLook + " not found in address book\n");
System.out.println("\nWould you like to add this person? [y or n] => ");
again = keybd.nextLine();
System.out.println(again);
if (again.equals("Y") || again.equals("y"))
addEntry(addrbook, keybd);
}
System.out.println("\nAnother Lookup? [y or n] => ");
again = keybd.nextLine();
}
while (again.equals("Y") || again.equals("y"));
// prompt the user to see if they want to see the entire address book before exiting
System.out.println("\nPrint the address book? [y or n] => ");
again = keybd.nextLine();
if (again.equals("Y") || again.equals("y"))
{
System.out.println();
numentries = addrbook.reset(INORDER);
for (i = 1; i <= numentries; i++)
{
tentry = addrbook.getNext(INORDER);
System.out.println(tentry);
}
}
}
// read entries from file and insert them into the address book
public static void buildAddrBook (BinarySearchTree<AddrEntry> addrbook)// here as well {
AddrEntry hold;
String name;
String street;
String town;
String phone;
int ok = 1;
try
{
FileReader freader = new FileReader("addrbook.dat");
BufferedReader addresses = new BufferedReader(freader);
name = addresses.readLine();
while (name != null)
{
street = addresses.readLine();
town = addresses.readLine();
phone = addresses.readLine();
hold = new AddrEntry(name, street, town, phone);
addrbook.add(hold);
name = addresses.readLine();
}
}
catch(
Exception e
)
{
}
}
// add a new entry to the address book
public static void addEntry (BinarySearchTree<AddrEntry> addrbook, Scanner keybd)//also here {
int ok;
AddrEntry hold;
String name;
String street;
String town;
String phone;
System.out.println("\nEnter name: ");
name=keybd.nextLine();
System.out.println("Enter street address: ");
street=keybd.nextLine();
System.out.println("Enter town, state, and zip: ");
town=keybd.nextLine();
System.out.println("Enter phone number: ");
phone=keybd.nextLine();
hold=new
AddrEntry (name, street, town, phone);
addrbook.add(hold);
}
AddrEntry类
public class AddrEntry implements Comparable<AddrEntry> {
private String name;
private String street;
private String town;
private String phone;
public AddrEntry() {}
public AddrEntry(String nn, String st, String tt, String ph) {
name = nn;
street = st;
town = tt;
phone = ph;
}
public void setEntry(String nn, String st, String tt, String ph) {
name = nn;
street = st;
town = tt;
phone = ph;
}
public String toString() {
return name + "\n" + street + "\n" + town + "\n" + phone;
}
public boolean equals(Object ptest) {
if (ptest == null || !(ptest instanceof AddrEntry))
return false;
return this.name.equals(((AddrEntry)ptest).name) &&
this.street.equals(((AddrEntry)ptest).street) &&
this.town.equals(((AddrEntry)ptest).town) &&
this.phone.equals(((AddrEntry)ptest).phone);
}
public int compareTo(AddrEntry ent) {
return name.compareTo(ent.name);
}
}