Java地址簿字符串阅读

时间:2015-11-15 20:12:38

标签: java

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

AddessBook Class

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]);
    }
}
}

联系班级

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;
    }}}}

3 个答案:

答案 0 :(得分:2)

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

firstName

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

答案 1 :(得分:1)

当您调用“removeContact”方法时,您只作为参数传递名称,而不是姓氏;假设您创建了联系人“John Doe”。当您搜索“John Doe”时,您将“John”作为方法的名称,但“Doe”是扫描程序中的下一个标记,因此当您尝试执行s.nextInt()时会出现问题,因为“Doe”不是一个int。

此外,当您有多个联系人时,您的removeContact方法无效。如果地址簿的第一次联系人不是您要查找的地址簿,则表示您正在返回。

编辑:

试试这个,让我知道会发生什么:

case 3:
System.out.println("Enter in full name: ");
String firstName = s.next();
String lastName = s.next();
addressbook.removeContact(firstName + " " + lastName);
break;

编辑2:

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

答案 2 :(得分:0)

好的,我运行了你的代码并试图删除一个名字,所以我收到了这个错误:

  

线程中的异常&#34; main&#34; java.util.InputMismatchException

您的remove方法不正确。您可以轻松地在地址簿中找到17个Bob - &#34; Bob Smith&#34;,&#34; Bob Jacobs&#34;,&#34; Bob Ahmed&#34; ,无论......

您需要修复removeContact方法,使其看起来更像这样:

public boolean removeContact(String fname, String lname){
    for(int i = 0; i<count; i++){
        if(contacts[i].getFirstName().equals(fname) && contacts[i].getLastName().equals(lname)){
            for(int j = i; j<count; j++){
                contacts[j] = contacts[j+1];
            }

此外,Main的case 3应该像这样:

case 3:
    System.out.println("Enter in first name: ");
    String firstname = s.next();
    System.out.println("Enter in last name: ");
    String lastname = s.next();
    addressbook.removeContact(firstname, lastname);
    break;