如何用字符串按字母顺序排列电话簿

时间:2019-04-30 18:43:18

标签: java

我有我的PhoneBook程序,但是当用户为条目列表输入“ l”时,我试图使条目自动按字母顺序排列。我不知道该怎么做。我尝试过将其放入列表中进行排序,但它仅使用条目的名字。

import java.io.*;
import java.util.*;
class Entry {
    public String fname, number, note, lname;
}
public class Main {
    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;
        readPhoneBook("PhoneBook.txt");
        System.out.println("Please Enter A Command.\nUse" +
                " \"e\" for enter," +
                " \"f\" for find," +
                " \"l\" for listing all the entries," +
                " \"m\" to merge duplicate entries," +
                " \"d\" to delete an entry," +
                " \"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 'm':

                    break;
                case 'd':

                    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.next(); //First Name
        stdin.nextLine();
        System.out.print("Enter Last Name: ");
        String lname = stdin.next(); //Last Name
        String number;
        stdin.nextLine();
        contactList[num_entries] = new Entry();
        contactList[num_entries].fname = fname; //Saves first name as fname
        contactList[num_entries].lname = lname; //Saves last name as lname
        System.out.print("Enter Number: ");
        number = stdin.nextLine();
        contactList[num_entries].number = number; //Saves phone number as number
        System.out.print("Enter Notes: ");
        contactList[num_entries].note = stdin.nextLine(); //saves any notes
        num_entries++;
    }
    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].fname.equalsIgnoreCase(Key))
                return i; // Found the Key, return index.
        }
        return -1;
    }
    public static void displayContact(Entry contact) {
        System.out.println("--"+ contact.fname+"\t"+
                contact.lname+"\t"+
                contact.number+"\t"+
                contact.note);
    }
    public static void listAllContacts() {
        int i = 0;
        while (i < num_entries) {
            displayContact(contactList[i]);
            i++;
        }
    }
    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);
        }
    }}

这是用于输入新联系人的位

public static void addContact() {
        System.out.print("Enter First Name: ");
        String fname = stdin.next(); //First Name
        stdin.nextLine();
        System.out.print("Enter Last Name: ");
        String lname = stdin.next(); //Last Name
        String number;
        stdin.nextLine();
        contactList[num_entries] = new Entry();
        contactList[num_entries].fname = fname; //Saves first name as fname
        contactList[num_entries].lname = lname; //Saves last name as lname
        System.out.print("Enter Number: ");
        number = stdin.nextLine();
        contactList[num_entries].number = number; //Saves phone number as number
        System.out.print("Enter Notes: ");
        contactList[num_entries].note = stdin.nextLine(); //saves any notes
        num_entries++;
    }

1 个答案:

答案 0 :(得分:1)

我将在您的Entry类中创建一个compareTo方法,并将其设置为比较您想要的方法。不过,最重要的部分是您如何存储条目。如果您使用已排序的数组,则可以对所有内容进行排序,然后将其输入到数组中,并在想要读取时线性展开