字符串搜索与“startsWith()”
问题描述:
我一直在一个电子邮件目录程序的过去几天,在我的方法之一,我试图做一个搜索功能,搜索电子邮件基于关闭字符输入的用户。我试图让它到达方法循环的地方,并且用户一次只输入一个字符,直到我为此方法构建的数组中只有一个电子邮件。字符串搜索与“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”来搜索“标记”:
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]
I t在另一个的外侧做另一个循环,这个循环也计算并使用它来移动给定字符串的索引,但是失败了。我觉得我很接近但忽略了一些东西。任何意见或策略将不胜感激!太感谢了。
答
假设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.");
}
感谢您的编辑。你期望输出什么?我想它应该要求更多的信件,如果它发现多个邮件? – Tom
在你的代码中:'for(int i = 0; i
Forseth11
为了更清楚地说明,在添加四条记录后,您可以添加另一个包含搜索的预期输出的块吗?就像在你当前的输出中那样,你输入了“2”,但是有了预期的结果。谢谢。 – Tom