【问题标题】:Search is skipping first element in linked list in Java搜索正在跳过Java中链表中的第一个元素
【发布时间】:2016-09-04 21:04:55
【问题描述】:

这里是初级程序员,我的程序的搜索功能有问题,除非您搜索列表中的第一项,否则它就像一个魅力。一点背景知识,这个程序(学校作业),读入一个 CSV 文件,创建一个链接列表和哈希表,并对列表进行排序。对于这个任务,我们必须允许用户按他们的名字搜索贡献者。如前所述,它可以正常工作,直到您搜索列表中的名字 George。我添加了打印行以打印迭代器的当前值,输出显示 George 实际上是第一个元素,但是当您搜索该名称时,它会出现“未找到”。这是我用于搜索的代码 sn-p:

      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 Gordon 让
迈克
蒂姆

抱歉,找不到该名称!

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

【问题讨论】:

  • 为什么投反对票??

标签: java search linked-list iterator skip


【解决方案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!");
  }

【讨论】:

  • 进行了所有更改,仍然无法正常工作。感谢您提供关于不制作额外变量的提示……但是仍然得到相同的结果,所有其他名称都显示得很好,只有第一个(乔治)没有……它可能在我的代码中的其他地方吗?这是一个开关内的案例......真的不知道发生了什么......
  • 在给您留下第一印象之前分配给数据贡献者?
  • 试过了...这听起来很奇怪,但我认为这是 .csv 文件中的“George”的问题....它没有跳过第一项,我手动输入了其他贡献者,甚至命名了几个乔治,我可以搜索和打印它们就好了......如果我手动输入一个将在列表顶部的名称(它被排序),我也可以搜索并找到它...这只是我无法搜索的特定乔治线....但这没有任何意义:/
  • 我修改了我的答案,添加 .trim() 去掉可能有字符串的空格
  • 如果可以的话,我会吻你……你把contributorData.getFirstName()放在那里两次……但不管怎样,这行得通。所以,只是为了让我明白这里发生了什么......trim() 去掉了空格,所以 George 数据的名称中有一个空格?没有意义,因为即使在 excel 文件中也没有空格,它们都是一样的......我的意思是,嘿,它有效......只是很奇怪,我不完全明白为什么
【解决方案2】:

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

希望你能得到结果。

【讨论】:

  • 感谢您的努力,但没有奏效。一旦找到名称,它就会退出 While 循环......列表中的所有其他名称都可以正常工作,它会打印名称和所有内容,然后返回到主菜单(这是 switch 语句中的一种情况) .我在你建议的地方添加了 Break,但不幸的是它没有改变任何东西......
  • 乔治在您的链表第 0 个索引处。如果可以,请尝试用增强的 4 循环替换您的迭代器。我相信您没有对此列表执行任何删除操作。也可以使用增强型。 for(贡献者 cbtr:conteibutorList){。 if(searcName.equals(cbtr.getName){. //找到结果} }
  • 我是一个非常初学者的程序员,唯一的经验是以前的课程......从来没有做过增强的 4 循环......不过我会查一下,看看能不能让它工作有了这个,谢谢
  • 是的,George 从第 0 个索引开始......但是,即使我通过手动输入新贡献者将不同的贡献者放入第 0 个索引中,我也可以很好地搜索那个.. .George在哪里并不重要,程序说他不存在......即使当我打印他的名单时。我可以使用哈希表搜索他的 ID 并找到他......这很奇怪,让我脱发......
  • 我确信你的 if 条件结果 == false 有问题,你能不能请你做这个条件 if(!result)。 &试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多