beginner programmer here and I am having an issue with the search function of my program, it works like a charm unless you search for the first item in the list. A little background, this program (school assignment), reads in a CSV file, creates a Linked List and Hash Table, and sorts the list. For this assignment we have to allow users to search for a contributor by their first name. As stated before, it works just fine until you search for the first name on the list, George. I added in print lines to print the current value of the iterator, and the output shows that George is infact the first element, however when you search that name it comes up "not found". Here is my code snippet for the search:
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();
Here is the output when I try to search for George:
George <-- current value at iterator
Enter the first name of the contributor you are searching:
George
George <--This shows that the value at the iterator should be George, so wth?
Gordon <--Why is search starting here??
Jean
Mike
Tim
Sorry, but that name was not found!
So yeah, I'm very lost as to what is happening here. I've tried moving the contributorData = nameIter.next(); around, but it doesn't fix the problem or it causes more...any help here would be greatly appreciated.
the route of the iterator on your list is fine, what may be happening is that your data are spacios blank and if it does not get rid of them affects the comparison and therefore can not find it, also verify if it is to reverse order
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!");
}