搜索正在跳过 Java 中链表中的第一个元素

Search is skipping first element in linked list in Java

这里是初学者程序员,我的程序的搜索功能有问题,除非您搜索列表中的第一项,否则它就像一个魅力。一点背景知识,这个程序(学校作业)读取一个 CSV 文件,创建一个链接列表和哈希 Table,并对列表进行排序。对于此任务,我们必须允许用户通过他们的名字搜索贡献者。如前所述,在您搜索列表中的第一个名字 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 <-- 迭代器的当前值
输入您要搜索的贡献者的名字:
乔治
George <--这表明迭代器的值应该是 George,所以 wth?
Gordon <--为什么搜索从这里开始??

麦克
蒂姆

抱歉,找不到该名称!

是的,我对这里发生的事情感到很迷茫。我试过移动 contributorData = nameIter.next();周围,​​但它不能解决问题或导致更多问题...任何帮助将不胜感激。

程序流程看起来不错,除了我发现的一个问题,您的 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.

希望你能得到结果。

你列表中迭代器的路径是好的,可能发生的是你的数据是空白的,如果不去掉它们会影响比较,因此找不到它,还要验证它是倒序

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!");
  }