Java 使用“startsWith()进行字符串搜索”
在过去的几天里,我一直在开发一个电子邮件目录程序,在我的一个方法中,我尝试使用一个搜索功能,根据用户的字符输入搜索电子邮件。我试图使它成为方法循环的地方,用户在电子邮件中一次键入一个字符,直到我为此方法构建的数组中只有一封电子邮件 这是我的密码:Java 使用“startsWith()进行字符串搜索”,java,loops,Java,Loops,在过去的几天里,我一直在开发一个电子邮件目录程序,在我的一个方法中,我尝试使用一个搜索功能,根据用户的字符输入搜索电子邮件。我试图使它成为方法循环的地方,用户在电子邮件中一次键入一个字符,直到我为此方法构建的数组中只有一封电子邮件 这是我的密码: private void searchContact() { String[] newRecords=new String[emailRecords.size()]; //temp array for searching
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
}
这里是我输入信息并尝试通过输入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数据结构来存储电子邮件地址。trie的一个常见应用是存储预测文本或自动完成字典。。。从 谢谢你的编辑。你期望什么样的产出?我想如果它在你的代码中发现了不止一封邮件,它应该要求更多的信件;更清楚地说,在添加了四条记录之后,是否可以添加另一个包含搜索预期输出的块?就像在当前输出中,您输入了2,但得到了预期的结果。非常感谢。
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.");
}