有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用“startsWith()进行java字符串搜索”

在过去的几天里,我一直在开发一个电子邮件目录程序,在我的方法之一中,我试图创建一个搜索功能,根据用户的字符输入搜索电子邮件。我试图让方法循环,用户每次在电子邮件中键入一个字符,直到我为这个方法构建的数组中只有一封电子邮件

这是我的密码:

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

这里是我输入信息并试图通过输入“m”、“a”、“r”和“k”来搜索“mark”后的预期输出:

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) 个答案

  1. # 1 楼答案

    假设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.");
    }
    
  2. # 2 楼答案

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