在地址簿中添加和检查 Java 条目

发布于 2024-10-19 02:04:56 字数 686 浏览 7 评论 0原文

我有一个地址簿程序,它可以:

  1. 添加条目
  2. 删除条目
  3. 更新条目
  4. 查看所有条目
  5. 查看特定条目

所有功能都很好,但我的问题是我想在用户输入 NAME 后进行检查如果它已经存在

这是我的addEntry方法:

public void addEntry() {

        entry[counter] = new AddressBookEntry();
        entry[counter].setName(getNonBlankInput("Enter Name: "));
        entry[counter].setAdd(getNonBlankInput("Enter Address: "));
        entry[counter].setPhoneNo(getNonBlankInput("Enter Phone No.: "));
        entry[counter].setEmail(getNonBlankInput("Enter E-mail Address: "));
        counter++;

}

请帮助我添加一些过滤用户输入的条件。例如,如果用户输入的名称已存在。

提前致谢

I have an Address Book program it can:

  1. Add an Entry
  2. Delete an Entry
  3. Update an Entry
  4. View all Entries
  5. View specific entry

All functions well, but my problem is I want to check once the user has inputted a NAME if it already exists

This is my addEntry method:

public void addEntry() {

        entry[counter] = new AddressBookEntry();
        entry[counter].setName(getNonBlankInput("Enter Name: "));
        entry[counter].setAdd(getNonBlankInput("Enter Address: "));
        entry[counter].setPhoneNo(getNonBlankInput("Enter Phone No.: "));
        entry[counter].setEmail(getNonBlankInput("Enter E-mail Address: "));
        counter++;

}

Please help me to add some conditions that will filter the user input. Like, if the user inputted a name ALREADY EXISTS.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好多鱼好多余 2024-10-26 02:04:56

使用 HashMap 作为存储,Keys 为 用户名。您可以通过执行 containsKey 方法。另外,建议

  1. 检查 NULL 输入
  2. 检查空白输入
  3. 检查荒谬的名称(例如数字名称,如果不允许)
  4. 以相同的大小写形式存储键,即小写或大写。在查找重复项时,请确保您的输入已转换为该大小写。

//this is your address-book with unique User-Name
private static final HashMap<String, AddressBookEntry> addressBook = new HashMap<String, AddressBookEntry>();
...
...
boolean addEntry(){
    boolean isNewEntry = true;
    //getNonBlankInput should check for valid name
    String name = getNonBlankInput("Enter Name: ");
    if(!addressBook.containsKey(name.toLowerCase())){
        AddressBookEntry entry  = new AddressBookEntry();
        entry.setName(name);
        entry.setAdd(getNonBlankInput("Enter Address: "));
        entry.setPhoneNo(getNonBlankInput("Enter Phone No.: "));
        entry.setEmail(getNonBlankInput("Enter E-mail Address: "));
        addressBook.put(name.toLowerCase(), entry);
    }else{
        isNewEntry = false;
    }
    return isNewEntry;

}

Use HashMap as storage with Keys as UserName. You can check if user exists by performing containsKey method of HashMap. Also, it is advisable to

  1. Check for NULL input
  2. Check for blank input
  3. Check for absurd name (like numeric names, if not allowed)
  4. Store keys in same case i.e. either in lower-case or in upper case. And while looking for duplicate make sure that your input has converted to that case.

//this is your address-book with unique User-Name
private static final HashMap<String, AddressBookEntry> addressBook = new HashMap<String, AddressBookEntry>();
...
...
boolean addEntry(){
    boolean isNewEntry = true;
    //getNonBlankInput should check for valid name
    String name = getNonBlankInput("Enter Name: ");
    if(!addressBook.containsKey(name.toLowerCase())){
        AddressBookEntry entry  = new AddressBookEntry();
        entry.setName(name);
        entry.setAdd(getNonBlankInput("Enter Address: "));
        entry.setPhoneNo(getNonBlankInput("Enter Phone No.: "));
        entry.setEmail(getNonBlankInput("Enter E-mail Address: "));
        addressBook.put(name.toLowerCase(), entry);
    }else{
        isNewEntry = false;
    }
    return isNewEntry;

}
夜巴黎 2024-10-26 02:04:56

使用以名称为键的 HashMap 怎么样?然后,您可以在添加条目之前使用 containsKey() 来查看该名称是否已在 HashMap 中。

What about using a HashMap, keyed on the name? Then you could use containsKey() to see if the name's already in the HashMap before adding the entry.

谈下烟灰 2024-10-26 02:04:56

您需要将 getNonBlankInput 的结果存储在一些局部变量中,以便您可以在将它们直接添加到地址簿之前检查它们。您将需要某种循环来检查地址簿的所有条目并比较名称以查看它是否已包含在地址列表中。

You will want to store the results of getNonBlankInput in some local variables so you can check them before directly adding them into the address book. You will need a loop of some sort to check all the entries of the address book and compare the name to see if it is already contained in the address list.

夕嗳→ 2024-10-26 02:04:56
  1. 覆盖 AddressBookEntry 中的 equals(...) 和 hashCode()基于 name 属性,
  2. 使用 HashSet 来存储 AddressBookEntry 而不是数组,
  3. 使用 contains(...) 方法来查看该对象是否已经存在。 HashSet 的 大 O 为 O(1)包含
  1. override equals(...) and hashCode() in AddressBookEntry based on the name property
  2. use HashSet to store AddressBookEntry instead of an array
  3. use contains(...) method to see if this object already exisits. HashSet has a big O of O(1) for contains
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文