beginner 프로그래머가 여기 있습니다. 내 프로그램의 검색 기능에 문제가 있습니다. 목록의 첫 번째 항목을 검색하지 않으면 매력처럼 작동합니다. 작은 배경,이 프로그램 (학교 과제)은 CSV 파일을 읽고 링크 된 목록과 해시 테이블을 만들고 목록을 정렬합니다. 이 과제를 위해 사용자는 이름으로 기고자를 검색 할 수 있어야합니다. 앞서 언급했듯이 George의 이름에서 검색 할 때까지 잘 작동합니다. 필자는 반복자의 현재 값을 출력하기 위해 인쇄 줄을 추가했으며, 결과는 George가 실제로 첫 번째 요소임을 보여 주지만, 그 이름을 검색하면 "찾을 수 없음"이 나타납니다. 나는 조지를 검색 할 때 여기 검색은 Java의 연결된 목록에서 첫 번째 요소를 건너 뛰고 있습니다.
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 여야 함을 보여줍니다.
고든 < - 여기에서 검색을 시작하는 이유는 무엇입니까?
장
마이크
팀
미안하지만, 그 이름을 찾을 수 없습니다!
그래, 나는 여기에서 무슨 일이 일어나고 있는지에 대해 매우 분실했다. 나는 contributorData = nameIter.next(); 주위에 있지만 문제를 해결하지 못하거나 더 많은 원인이 있습니다. 여기있는 도움은 크게 감사하겠습니다.
왜 아래로 투표합니까 ?? –