如何在java中搜索?
我们被要求制作一个学生和教师目录,能够添加、编辑和搜索C数据。在搜索部分,我的老师要求一个输出,它将显示与搜索中输入匹配的所有结果 样本输出:如何在java中搜索?,java,search,directory,find,Java,Search,Directory,Find,我们被要求制作一个学生和教师目录,能够添加、编辑和搜索C数据。在搜索部分,我的老师要求一个输出,它将显示与搜索中输入匹配的所有结果 样本输出: search name: ab output: 1. **ab**a, josep jr v. [IV - Garent] 2. **ab**acada, willie f. [III - Waling2x] 3. **ab**zal, louie b. [i - Mercury] Press the number for detai
search name: ab
output:
1. **ab**a, josep jr v. [IV - Garent]
2. **ab**acada, willie f. [III - Waling2x]
3. **ab**zal, louie b. [i - Mercury]
Press the number for details. Zero(0) back to the menu.
Press you choice:
我如何用这样的输出进行搜索
我已经有了
public void search(){
System.out.print("search name : ");
try {
searchName = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
searchCount = 0;//for search
for(int i = 0; i<count; i++){
if(personList.get(i).getName().equals(searchName)){//for search
searchCount = i;
break;
}
}
System.out.println(personList.get(searchCount).getName() +" "+ personList.get(searchCount).getAge());
}
String searchName = "";//for search, store search name
int searchCount = 0;//for search
使用StringcontainsString而不是StringequalObject。执行搜索计数=i;您应该保存在列表中找到的对象,然后打印列表,或者直接打印项目
for(int i = 0; i<personList.size(); i++){
if(personList.get(i).getName().contains(searchName)){
System.out.println(personList.get(i).getName() +" "+ personList.get(i).getAge());
}
}
或
将此代码行更改为::
if(personList.get(i).getName().contains(searchName)){
//for search
当且仅当该字符串包含指定的字符值序列时,java.lang.String.contains方法返回true
并且String.equals将字符串与指定的对象进行比较。当且仅当参数不为null且是表示与此对象相同字符序列的字符串对象时,结果才为真。对搜索使用contains而不是EQUALES如果类似,则使用以下示例输出:搜索名称:cruz输出:1。克鲁兹,拉斐尔f。[III-瓦林2X]2。克鲁兹塔纳,玛丽b。[i-水星]3。黛拉·克鲁兹,罗芬娜p。[IV-Garent]创建此示例输出有什么问题?我可以给你举个例子,但是我不知道你需要什么方法来获取你想要打印的信息。用户搜索了单词cruz,然后有一个名字有两个单词dela cruz。该名称应显示在搜索中,因为第二个单词与单词cruzt匹配,因为它包含检查字符串是否包含另一个字符串。我明白了。谢谢你的帮助。哦还有一件事,一旦你搜索了一个名字,是否可以编辑或更新该名字下的信息?
if(personList.get(i).getName().contains(searchName)){
//for search