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

搜索正在跳过Java中链表中的第一个元素,java,search,linked-list,iterator,skip,Java,Search,Linked List,Iterator,Skip,这里是初学者程序员,我对我的程序的搜索功能有一个问题,除非你搜索列表中的第一项,否则它就像一个魅力。一点背景知识,这个程序(学校作业),读入一个CSV文件,创建一个链表和哈希表,并对列表进行排序。对于此任务,我们必须允许用户按其名字搜索贡献者。如前所述,在您搜索列表上的名字George之前,它工作正常。我添加了打印行来打印迭代器的当前值,输出显示George实际上是第一个元素,但是当您搜索该名称时,它显示为“not found”。以下是我的搜索代码片段: Iterator<Co

这里是初学者程序员,我对我的程序的搜索功能有一个问题,除非你搜索列表中的第一项,否则它就像一个魅力。一点背景知识,这个程序(学校作业),读入一个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();
Iterator namiter=contributorList.Iterator();
布尔结果=假;
字符串搜索名称;
System.out.println(contributorData.getFirstName())//迭代器指向的是什么名称??
System.out.println(“输入您正在搜索的参与者的名字:”);
searchName=in.nextLine();
字符串fName=searchName.toLowerCase();
字符串键名;
while(namiter.hasNext()){
contributorData=namiter.next();
System.out.println(contributorData.getFirstName());//迭代器指向的是什么名称??
keyName=contributorData.getFirstName().toLowerCase();//keyName应等于上述值
if(fName.equals(keyName)){
结果=真;
System.out.println(“\n找到以下参与者:\n”);
System.out.printf(“%-10s%-10s%-8s%-15s%-17s%s%n”、“第一个”、“最后一个”,
“国家”、“电话”、“捐款”、“身份证”);
contributorData.Print();
}//end if语句
}//结束时
如果(结果==false){
System.out.println(“\nOrry,但找不到该名称!”);
}
System.out.println(“\n按enter键返回主菜单…\n”);
in.nextLine();
以下是我尝试搜索George时的输出:


George程序流程看起来不错,除了我发现的一个问题,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!");
  }
Iterator namiter=contributorList.Iterator();
布尔结果=假;
字符串搜索名称;
System.out.println(“输入您正在搜索的参与者的名字:”);
searchName=in.next().toLowerCase().trim()//无需使用另一个变量来下套管
//并添加.trim()以消除可能包含字符串的空格
while(namiter.hasNext()){
contributorData=namiter.next();
if(contributorData.getFirstName().trim().toLowerCase().equals(searchName)){
System.out.println(“\n找到以下参与者:\n”);
System.out.printf(“%-10s%-10s%-8s%-15s%-17s%s%n”、“第一个”、“最后一个”,
“国家”、“电话”、“捐款”、“身份证”);
contributorData.Print();
//break;如果您只想取消注释第一个匹配项
}           
}
如果(结果==false){
System.out.println(“\nOrry,但找不到该名称!”);
}

谢谢您的努力,但这不起作用。一旦找到了名称,它就会退出While循环……对于列表中的每个其他名称,它都可以正常工作,打印名称和所有内容,然后返回主菜单(这是switch语句中的一种情况)。我在你建议的地方添加了中断,但不幸的是,它没有改变任何东西……乔治在你的链接列表第0个索引中。如果是这样,请尝试用增强的4循环替换迭代器。我相信您没有在此列表上执行任何删除操作。也可以使用增强的。对于(Contributor cbtr:conteibutorList){.If(searcName.equals(cbtr.getName){//找到的结果}我是一个非常初级的程序员,唯一的经验是以前的课程…从来没有做过增强的4循环…不过我会查一下,看看我是否能让它工作,谢谢你,是的,George从第0个索引开始…但是,即使我手动输入一个新的贡献者,在第0个索引中放置一个不同的贡献者,我也可以搜索它一个很好…不管乔治在哪里,程序说他不存在…即使当我打印他存在的列表时。我可以利用哈希表搜索他的ID并找到他…这很奇怪,让我毛骨悚然…我确信你的条件result==false会有问题,你能确定这个条件吗n如果(!result)&尝试进行所有更改,仍然不起作用。感谢您提供的关于不生成额外变量的提示…但是仍然得到相同的结果,所有其他名称显示良好,只有第一个(george)难道…它可能在我的代码中的其他地方吗?这是一个开关内的案例…真的不知道发生了什么…在给你留下第一印象之前分配给Data contributor?试过了…这听起来很奇怪,但我认为这是“George”的问题在.csv文件中…它没有跳过第一项,我手动输入了其他贡献者,甚至命名了一些george,我可以搜索并打印它们…而且如果我手动输入一个将位于列表顶部的名称(它会被排序),我也可以搜索并找到它…这只是我无法搜索的特定george行…但是我