[英]String search with “startsWith()”
在過去的兩天里,我一直在研究電子郵件目錄程序,在一種方法中,我試圖建立一個搜索功能,以根據用戶的字符輸入來搜索電子郵件。 我試圖使其遍歷到方法循環的位置,並且用戶一次在電子郵件中鍵入一個字符,直到為該方法構造的數組中只有一封電子郵件。
這是我的代碼:
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循環,並使用它來移動給定字符串的索引,但是失敗了。 我覺得我已經接近了,但可以俯瞰某些地方。 任何建議或策略將不勝感激! 太感謝了。
假設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.");
}
您可能要嘗試的一件事是使用trie數據結構來存儲電子郵件地址。 來自http://en.wikipedia.org/wiki/Trie的 “ Trie的常見應用是存儲預想性文本或自動完成詞典...”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.