我必须在Java中创建一个通讯录。我陷入了一个领域。当我添加第二个地址时,它会使第一个地址为空。我有一些地方被注释掉了,但我还没有这样做,所以请忽略那些地方。我很困惑为什么第一个地址变为空。

import java.util.Scanner;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;
import java.io.FileNotFoundException;

class Program2 {

    static Scanner s = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("\nWelcome. Address book is loaded.");
        loop: while(true) {
            displayOptions();
            int choice = s.nextInt();
            switch(choice) {
                case 1:
                    System.out.println("**Choice 1**");
                    AddressBook.display();
                    break;

                case 2:
                    System.out.println("**Choice 2**");
                    System.out.print("First Name: ");
                    String firstName = s.next();
                    System.out.print("Last Name: ");
                    String lastName = s.next();
                    System.out.print("Phone Number: ");
                    String phone = s.next();
                    Contact test = new Contact(firstName, lastName, phone);
                    AddressBook.add(test);
                    break;

                case 3:
                    System.out.println("**Choice 3**");
                    break;

                case 4:
                    System.out.println("**Choice 4**");
                    break;

                case 5:
                    break loop;
            }
        }
        System.out.println("\nAddress book is saved to file.\n");
        System.out.println("Good bye.\n");
        System.exit(0);

    }

    private static void displayOptions() {
        System.out.println("\nWhat would you like to do?");
        System.out.println("  1) Display all contacts\n" +
                   "  2) Add a contact\n" +
                   "  3) Remove a contact\n" +
                   "  4) Search a contact\n" +
                   "  5) Exit");
        System.out.print("Your choice: ");
    }

}

class Contact {

    private String firstName;
    private String lastName;
    private String phone;

    public Contact(String firstName, String lastName, String phone) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.phone = phone;
    }

    public String getFirstName() {return firstName;}
    public String getLastName() {return lastName;}
    public String getPhone() {return phone;}

    public void setFirstName(String firstName) {this.firstName = firstName;}
    public void setLastName(String lastName) {this.lastName = lastName;}
    public void setPhone(String phone) {this.phone = phone;}

    /*public boolean equals(Object o) {
        if (o instanceof Contact) {
            Contact contacts = (Contact) o;
            return (firstName.equals(contacts.getFirstName()) &&
                lastName.equals(contacts.getLastName()));
        }
        return false;
    }*/

    public String toString() {
        return firstName + " " + lastName + "\t\t" + phone;
    }

}

class AddressBook {

    public final static int CAPACITY = 100;
    static private Contact[] contacts;
    static private int count = 0;
    static private String addressFile = "address.txt";
    static File file = new File(addressFile);

    public AddressBook(String addressFile) {
        this.addressFile = addressFile;
    }

    public static boolean add(Contact c) {
        contacts = new Contact[CAPACITY];
        if (count < CAPACITY) {
            contacts[count++] = c;
        }
        return false;
    }

    //public boolean remove(fullname) { }

    //public Contact search(fullname) { }

    public static void display() {
        System.out.println("Name\t\t\tPhone Number");
        System.out.println("-------------------------------------");
        for (int i=0; i<count; i++) {
            System.out.println(contacts[i]);
        }
        System.out.println("-------------------------------------");
    }

    /*public boolean load() {

        try {
        Scanner sF = new Scanner(file);
        while (s.hasNext()) {
            String line = s.nextLine();
            System.out.println(line);
        }
        s.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /*public boolean save() {
        try {
            FileWriter writer = new FileWriter(addressFile);
            writer.write();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }*/

    //public boolean contains(String firstName, String lastName) {
    //  return this.contacts.contains(firstName, lastName);
    //}

}

最佳答案

您的add方法将覆盖contacts数组,因此每次调用它时,对先前Contact的引用都会丢失:

public static boolean add(Contact c) {
    contacts = new Contact[CAPACITY]; // remove this line
    if (count < CAPACITY) {
        contacts[count++] = c;
    }
    return false;
}


代替删除的行,只初始化一次contacts数组一次:

static private Contact[] contacts = new Contact[CAPACITY];

10-08 01:34