字符串搜索" startsWith()"

时间:2015-04-22 23:48:11

标签: java loops

我过去几天一直在处理电子邮件目录程序,并且在我的一种方法中,我尝试使用搜索功能搜索基于字符输入的电子邮件用户。我尝试将其设置为方法循环的位置,并且用户一次在电子邮件中输入一个字符,直到我为此方法构建的数组中只有一封电子邮件。

继承我的代码:

private void searchContact()    
{
    String[] newRecords=new String[emailRecords.size()];        //temp array for searching
    ArrayList<String> searchRecords=new ArrayList<String>();    //to be passed to insertion sort
    newRecords=emailRecords.toArray(newRecords);                

    for(String Records: newRecords) 
    {
        Scanner search=new Scanner(System.in);                  //setup for user input
        String letter;
        String searchVal;

        System.out.println("Please enter the first letter of the email you're trying to find.");
        letter=search.nextLine();

        if (searchRecords.size()!=1)    
        {   
            for (int i=0; i<newRecords.length;i++)          //counter for indexes
            {
                searchVal=newRecords[i];                        //set temp value to set index

                if (searchVal.startsWith(letter))               //starts with boolean
                {
                    searchRecords.add(searchVal);               //add to temp array for later comparison
                }
            }
        }
        else    
        {
            break;                                              //break if one remains in the array.
        }
    }
    System.out.println(searchRecords);                          //TODO erase when finalizing
}

以下是当我运行程序输入以相同字母开头的名字时会发生什么:

Please enter the number of your option choice:
1. Add a new contact
2. Search for an exsisting contact
3. Exit
1
Please enter the email adress.
mark
***mark was successfully stored.***
Please enter the number of your option choice:
1. Add a new contact
2. Search for an exsisting contact
3. Exit
1
Please enter the email adress.
mike
***mike was successfully stored.***
Please enter the number of your option choice:
1. Add a new contact
2. Search for an exsisting contact
3. Exit
1
Please enter the email adress.
molly
***molly was successfully stored.***
Please enter the number of your option choice:
1. Add a new contact
2. Search for an exsisting contact
3. Exit
2
Please enter the first letter of the email you're trying to find.
m
Please enter the first letter of the email you're trying to find.
a
Please enter the first letter of the email you're trying to find.
r
[mark, mike, molly]
Please enter the number of your option choice:
1. Add a new contact
2. Search for an exsisting contact
3. Exit

在我输入信息并尝试搜索&#34;标记&#34;之后我的预期输出通过输入&#34; m&#34;,&#34; a&#34;,&#34; r&#34;和&#34; k&#34;:

Please enter the next letter of the email you're trying to find.
m
Please enter the next letter of the email you're trying to find.
a
Please enter the next letter of the email you're trying to find.
r
Please enter the next letter of the email you're trying to find.
k
[mark]

我试图在另一个外部创建另一个for循环,它也计算并使用它来移动给定字符串的索引但它失败了。我觉得我很近但却忽略了一些东西。任何建议或策略将不胜感激!万分感谢。

2 个答案:

答案 0 :(得分:0)

假设emailRecords包含您的所有电子邮件,您的任务就像:

private void searchContact() {
    assert(!(emailRecords == null || emailRecords.isEmpty()));// :P
    //initially copy all
    ArrayList<String> searchRecords = new ArrayList<>(emailRecords);
    //prepare scanner
    Scanner search = new Scanner(System.in);
    //initialize query
    String query = "";
    //loop:
    while (searchRecords.size() > 1) {
        System.out.println("Please enter the first letter of the email you're trying to find.");
        //read from input
        query += search.nextLine();
        //iterate through remaining searchRecords
        for (Iterator<String> it = searchRecords.iterator(); it.hasNext();) {
            final String entry = it.next();
            if (!entry.startsWith(query)) {//...conditionally
                it.remove();//..remove (from searchRecords)
            }
        }
    }
    //print output - first/last of searchRecords
    if (!searchRecords.isEmpty())
        System.out.println(searchRecords.get(0));
    else
        System.out.println("No record found.");
}

答案 1 :(得分:0)

您可能尝试的一件事是使用 trie 数据结构来存储电子邮件地址。 “trie的常见应用是存储来自http://en.wikipedia.org/wiki/Trie的预测文本或自动完成字典......”。