初学者程序员在这里,我遇到了程序搜索功能的问题,除非你搜索列表中的第一项,否则它就像魅力一样。一个小背景,这个程序(学校作业),读取CSV文件,创建链接列表和哈希表,并对列表进行排序。对于此分配,我们必须允许用户按其名字搜索贡献者。如前所述,在您搜索列表中的第一个名称George之前,它可以正常工作。我在打印行中添加了打印迭代器的当前值,并且输出显示George实际上是第一个元素,但是当您搜索该名称时,它会出现" not found"。以下是我搜索的代码段:
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&lt; - 迭代器的当前值
输入您要搜索的贡献者的名字:
乔治
George&lt; - 这表明迭代器的值应该是George,所以wth?
戈登&lt; - 为什么搜索从这里开始?
让
迈克
蒂姆
抱歉,但找不到该名称!
所以是的,我对这里发生的事情非常失落。我试过移动contributorData = nameIter.next();周围,但它没有解决问题或它导致更多...这里的任何帮助将不胜感激。
答案 0 :(得分:1)
你的列表上迭代器的路径很好,可能发生的是你的数据是空间空白,如果它没有摆脱它们影响比较,因此无法找到它,也验证它是否是逆序
Iterator<Contributor> nameIter = contributorList.iterator();
boolean result = false;
String searchName;
System.out.println("Enter the first name of the contributor you are searching: ");
searchName = in.next().toLowerCase().trim();//it is not necessary to use another variable to downcasing
//and add .trim () to get rid of blank spaces that may have strings
while (nameIter.hasNext()){
contributorData = nameIter.next();
if (contributorData.getFirstName().trim().toLowerCase().equals(searchName)){
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();
//break ;if you only want to get the first match uncomment
}
}
if (result == false){
System.out.println("\nSorry, but that name was not found!");
}
答案 1 :(得分:0)
程序流看起来不错,除了我发现的一个问题,你的while循环在满足条件时不会终止。我的意思是你在寻找乔治。一旦满足条件,就应该停止循环。
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();
break; // this will make the flow exit from the loop //whenever a condition is met.
}//end if statement.
我希望你能得到结果。