Java地址簿字符串读取

标签 java

我在使用 AddressBook 类中的 removeContact 方法时遇到了问题。当我输入名字时,我可以让它工作,但由于某种原因无法让它工作全名。这可能很简单,但我一无所获。谢谢

AddressBook类

public class AddressBook {
private Contact contacts[] = new Contact[100]; //Array to hold all the contacts
private int count = 0; //Number of contacts in the array(in address book)
private String fileName;

AddressBook(String fileName){
    this.fileName = fileName;
}

//Add Contact
public boolean addContact(Contact contact){
    if(count<100){
        contacts[count] = contact;
        count++;
        return true;
    }else{
        return false;
    }
}



//Remove Contact
public boolean removeContact(String fullname){
    for(int i = 0; i<count; i++){
        if(fullname.equals(contacts[i].getFullName())){
            for(int j = i; j<count; j++){
                contacts[j] = contacts[j+1];
            }
            count--;
        }else{
            return false;
        }
    }
    return true;
}

//Search Contact

//Display Contacts
public void displayContacts(){
    for(int i = 0; i<count; i++){
        System.out.println(contacts[i]);
    }
}
}

联系类(class)

public class Contact {
private String firstName;
private String lastName;
private String phone;


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



//First name getter
public String getFirstName(){
    return firstName;
}
//Last name getter
public String getLastName(){
    return lastName;
}
//Full name getter
public String getFullName(){
    return firstName+" "+lastName;
}
//Phone number getter
public String getPhone(){
    return phone;
}
//First name mutator
public void setFirstName(String firstName){
    this.firstName = firstName;
}
//Last name mutator
public void setLastName(String lastName){
    this.lastName = lastName;
}
//Phone number mutator
public void setPhone(String phone){
    this.phone = phone;
}
//Method to compare first and last names for similarity
public boolean compare(Object o){
    Contact contact = (Contact) o;
    if(firstName == contact.firstName && lastName == contact.lastName){
        return false;
    }else{
        return true;
    }
}

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

主类

import java.util.*;
public class Main {
public static void main(String [] args){

    AddressBook addressbook = new AddressBook("Info.txt");

    Scanner s = new Scanner(System.in);

    boolean quit = false;
    while(!quit){
        System.out.println("What would you like to do?");
        System.out.println(" 1) Display all contacts");
        System.out.println(" 2) Add a contact");
        System.out.println(" 3) Remove a contact");
        System.out.println(" 4) Search a contact");
        System.out.println(" 5) Exit");

        switch(s.nextInt()){
        case 1: 
            addressbook.displayContacts();
            break;
        case 2:
            System.out.println("First Name: ");
            String fName = s.next();
            System.out.println("Last Name: ");
            String lName = s.next();
            System.out.println("Phone Number: ");
            String pNumber = s.next();
            addressbook.addContact(new Contact(fName, lName, pNumber));
            break;
        case 3:
            System.out.println("Enter in full name: ");
            String fullname = s.next();
            addressbook.removeContact(fullname);
            break;
        case 4:
            System.out.println("Enter the name of the contact you are looking for: ");
        case 5:
            quit = true;
            break;
    }}}}

最佳答案

您要求提供全名,然后仅将其与名字进行比较:

if(fullname.equals(contacts[i].getFirstName())){

只需将其与全名进行比较...

关于Java地址簿字符串读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33724478/

相关文章:

java 如何在同名目录中创建.txt

java - Servlet 与 Vaadin 7 应用程序一起运行吗?

java - 如何从Intent接收文件路径

java - Rome XmlReader 不读取 https 提要

Java 迭代器和泛型类型转换

java - 500 java.lang.OutOfMemoryError 后重定向

java - 在映射中正确使用泛型

java - Xstream:具有 XStreamImplicit 的 itemFieldName 但仍包裹在元素中的列表

java - 语音转文本时如何只返回一个结果?

java - 多个 Java Web 应用程序的自动化部署解决方案