【发布时间】:2016-09-04 21:04:55
【问题描述】:
这里是初级程序员,我的程序的搜索功能有问题,除非您搜索列表中的第一项,否则它就像一个魅力。一点背景知识,这个程序(学校作业),读入一个 CSV 文件,创建一个链接列表和哈希表,并对列表进行排序。对于这个任务,我们必须允许用户按他们的名字搜索贡献者。如前所述,它可以正常工作,直到您搜索列表中的名字 George。我添加了打印行以打印迭代器的当前值,输出显示 George 实际上是第一个元素,但是当您搜索该名称时,它会出现“未找到”。这是我用于搜索的代码 sn-p:
Iterator<Contributor> nameIter = contributorList.iterator();
boolean result = false;
String searchName;
System.out.println(contributorData.getFirstName()); //What Name is the iterator pointing at??
System.out.println("Enter the first name of the contributor you are searching: ");
searchName = in.nextLine();
String fName = searchName.toLowerCase();
String keyName;
while (nameIter.hasNext()){
contributorData = nameIter.next();
System.out.println(contributorData.getFirstName()); //What Name is the iterator pointing at??
keyName = contributorData.getFirstName().toLowerCase(); //keyName should equal the above value
if (fName.equals(keyName)){
result = true;
System.out.println("\nThe following contributor was found:\n");
System.out.printf("%-10s %-10s %-8s %-15s %-17s %s %n", "First", "Last",
"Country", "Phone #", "Contribution", "ID");
contributorData.Print();
}//end if statement
}//end While
if (result == false){
System.out.println("\nSorry, but that name was not found!");
}
System.out.println("\nPress enter to return to the Main Menu...\n");
in.nextLine();
这是我尝试搜索 George 时的输出:
George
输入您要搜索的贡献者的名字:
乔治
George
Gordon
让
迈克
蒂姆
抱歉,找不到该名称!
所以是的,我对这里发生的事情感到非常迷茫。我试过移动contributorData = nameIter.next();周围,但它不能解决问题或导致更多...这里的任何帮助将不胜感激。
【问题讨论】:
-
为什么投反对票??
标签: java search linked-list iterator skip