在我的程序中使用find方法的问题

时间:2018-04-26 03:07:26

标签: java list find

我正在尝试使用find函数来查找被调用的索引,以查找输入的第一个名称保存的每个条目。我的程序打印出我想要查找的联系人,但随后也打印出输入的整个联系人列表。我找不到原因。我一直在尝试不同的东西,但它只打印第一个条目或整个列表。非常感谢任何帮助。

到目前为止,这是我的代码:

class Entry {
public String fname, lname, number, note;
}
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[999];
    num_entries = 0;
    try {
        readPhoneBook("PhoneBook.txt");
    } catch (FileNotFoundException e) {}
    System.out.println("Codes are entered as 1 to 8 characters.\nUse" +
            " \"e\" for enter," +
            " \"f\" for find," +
            " \"l\" for listing all the entries," +
            " \"m\" for removing the entries by name," +
            " \"d\" for removing the entries by phone number," +
            " \"a\" for sort alphabetically by first name," +
            " \"n\" for sort alphabetically by last name," +
            " \"p\" for sort by number," +
            " \"q\" to quit.");
    Command = null;
    C = ' ';
    while(true) { // loop infinitely

        System.out.print("Command: ");
        Command = stdin.nextLine();
        C = Command.charAt(0);
        switch (C) {
            case 'e': addContact(); break;
            case 'f':
                code = stdin.next();
                stdin.nextLine();
                i = index(code);
            case 'l':
                listAllContacts(); break;
            case 'q': // when user wants to quit
                CopyPhoneBookToFile("PhoneBook.txt");
                System.out.println("Quitting the application. All the entries are "
                        + "stored in the file PhoneBook1.txt");
                System.exit(0); // simply terminate the execution
            case 'a':
                sortList1();
                break;
            case 'n':
                sortList2();
                break;
            case 'p':
                sortListByPhoneNumber();
                break;
            case 'm': // m for deleting a contact; delete by first name
                System.out.print("Enter the first name of a contact you wish to remove : ");
                String fname = stdin.nextLine(); // read the contact number
                removeEntry(fname); // remove the number from the entries
                break;
            case 'o': // m for deleting a contact; delete by last name
                System.out.print("Enter the last name of a contact you wish to remove : ");
                String lname = stdin.nextLine(); // read the contact number
                removeEntry2(lname); // remove the number from the entries
                break;
            case 'd': // m for deleting a contact; delete by phone number
                System.out.print("Enter the number of a contact you wish to remove : ");
                String number = stdin.nextLine(); // read the contact number
                removeEntry1(number); // remove the number from the entries
                break;
            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].fname = S.next();
        contactList[num_entries].lname = S.next();
        contactList[num_entries].number = S.next();
        contactList[num_entries].note = S.nextLine();
        num_entries++;
    }
    S.close();
}
public static void addContact() {
    System.out.print("Enter first name: ");
    String fname = stdin.nextLine();
    String lname;
    String number;
    String pattern = "^\\(?(\\d{3})?\\)?[- ]?(\\d{3})[- ](\\d{4})$";
    while (fname.length() > 8 || fname.length() < 1) {
        System.out.println("First name must be between 1 to 8 characters.");
        System.out.print("Enter first name: ");
        fname = stdin.nextLine();
    }
    contactList[num_entries] = new Entry();
    contactList[num_entries].fname = fname;
    System.out.print("Enter last name: ");
    lname = stdin.nextLine();
    while (lname.length() > 8 || lname.length() < 1) {
        System.out.println("First name must be between 1 to 8 characters.");
        System.out.print("Enter first name: ");
        lname = stdin.nextLine();
    }
    contactList[num_entries].lname = lname;
    System.out.print("Enter Number: ");
    number = stdin.nextLine(); // read the number

    while(!number.matches(pattern)) { // as long as user doesnt enters correct format, loop
        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();
    }
    contactList[num_entries].number = number;

    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
    Key=Key.toLowerCase();
    System.out.println(Key);
    for (int i=0; i < num_entries; i++) {
        if (contactList[i].fname.equals(Key)) {
            if (i >= 0) displayContact(contactList[i]);
            //System.out.println();
            //return i;
        }    // Found the Key, return index.
    }
    return -1;
}
public static void displayContact(Entry contact) {
    System.out.println("--"+ contact.fname+"\t");
    System.out.println("--"+ contact.lname+"\t");
    System.out.println("--"+ contact.number+"\t");
    System.out.println("--"+ contact.note);
    System.out.println("");
}
public static void listAllContacts() {
    for(Entry e : contactList) {
        if(e != null)
            displayContact(e);
        else
            break;
    }
}
public static void sortList1() {
    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].fname.compareToIgnoreCase(contactList[i].fname)> 0) {
                temp = contactList[j];
                contactList[j] = contactList[i];
                contactList[i] = temp;
            }
        }
    }listAllContacts();
}
public static void sortList2() {
    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].lname.compareToIgnoreCase(contactList[i].lname)> 0) {
                temp = contactList[j];
                contactList[j] = contactList[i];
                contactList[i] = temp;
            }
        }
    }listAllContacts();
}
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].fname + "\t" +
                        contactList[i].lname + "\t" +
                        contactList[i].number + "\t" +
                        contactList[i].note);
    }
}
private static void removeEntry(String fname) {
    Entry[] newcontactList = new Entry[contactList.length];
    int i = 0;
    for(Entry e : contactList) {
        if(e == null) break; // if an entry is null then break the loop
        if(e.fname.equals(fname)) // if the given number matches the current number
            continue; // then skip
        newcontactList[i++] = e;
    }
    num_entries--; // decrease the number of entries by 1;
    contactList = newcontactList;
}
private static void removeEntry2(String lname) {
    Entry[] newcontactList = new Entry[contactList.length];
    int i = 0;
    for(Entry e : contactList) {
        if(e == null) break; // if an entry is null then break the loop
        if(e.lname.equals(lname)) // if the given number matches the current number
            continue; // then skip
        newcontactList[i++] = e;
    }
    num_entries--; // decrease the number of entries by 1;
    contactList = newcontactList;
}
private static void removeEntry1(String number) {
    Entry[] newcontactList = new Entry[contactList.length];
    int i = 0;
    for(Entry e : contactList) {
        if(e == null) break; // if an entry is null then break the loop
        if(e.number.equals(number)) // if the given number matches the current number
            continue; // then skip
        newcontactList[i++] = e;
    }
    num_entries--; // decrease the number of entries by 1;
    contactList = newcontactList;
}
private static void sortListByPhoneNumber() {
    int i;
    Entry temp;
    for (int j = 0; j < num_entries; j++) {
        for (i = j + 1; i < num_entries; i++) {
            if (contactList[j].number.compareToIgnoreCase(contactList[i].number) > 0) {
                temp = contactList[j];
                contactList[j] = contactList[i];
                contactList[i] = temp;
            }
        }
    }
    listAllContacts();
}
}

1 个答案:

答案 0 :(得分:0)

你忘记了break;

case 'f':
                code = stdin.next();
                stdin.nextLine();
                i = index(code);
case 'l':
                listAllContacts(); break;

因此,您的查找f会进入您的列表l

只需在您的案例之前添加一个休息时间,这样两者就不会相互碰撞。