我們被要求創建一個能夠添加,編輯和搜索數據的學生和教師目錄。在搜索部分,我的老師要求一個輸出,它將顯示與搜索中的輸入相匹配的所有結果。如何在java中搜索?
輸出樣本:
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
使用'contains',而不是'equals'的搜索 – BackSlash