【问题标题】:Searching through array of objects with user input, validating input使用用户输入搜索对象数组,验证输入
【发布时间】:2015-12-17 12:22:04
【问题描述】:

我正在使用 Java 中的这两种方法验证输入。 searchCandidate 方法搜索已排序的数组以查找名称与用户输入(字符串 searchKey)匹配的对象。 displayCandidate 方法调用此方法,检查验证,并返回该特定对象的所有值。

在 searchCandidate(直接在下面)方法中,如果找到匹配项,则返回对象的副本。如果没有,则返回一个所有字段都为“null”的新对象。

   //Find user input candidate location in array 
     public static Candidate searchCandidate(Candidate[] candidate)
      {
        String currName;
        int first = 0,
            last = candidate.length-1,
            middle;
        Scanner kbd = new Scanner(System.in);
        String searchKey = kbd.nextLine();
        searchKey = searchKey.toUpperCase();

        while(first <= last)
         {
            middle = (first + last)/2;
            currName = candidate[middle].getName();
            if(currName.equals(searchKey))
               {
                  return new Candidate(candidate[middle]);
               }
            else if(currName.compareTo(searchKey)<0)
               {
                  first=middle+1;
               }
            else
               {
                  last = middle - 1;
               }
         }

    return new Candidate("null","null","null","null");
}

在 displayCandidate 方法中,它检查 searchCandidate 创建的对象的 name 字段是否等于“null”,然后返回一个字符串,通知用户没有匹配的候选人。如果不等于null,则返回所有对象值。

   //Display candidates info after search
   public static String displayCandidate(Candidate[] candidate)
    {
        System.out.println("Please enter the name of the candidate (LAST,FIRST)");                                      
        Candidate candidateChoice = searchCandidate(candidate);
        if(candidateChoice.getName().equals("null"))
          {
              return "There are no candidates with this name.";
          }
          return candidateChoice.display();
    }

当我运行并输入不在数组中的对象的名称时,不会返回任何内容。它只是循环回到我的用户菜单。知道为什么会这样吗?

【问题讨论】:

  • 最好用您正在使用的编程语言添加标签
  • @alfasin 不小心添加了。谢谢。
  • 在输入 if 之前尝试将 CandidateChoice.getName() 打印到您的控制台,以确保将“null”更改为其他内容以确保它引用的是字符串而不是状态
  • @YassinHH 我将“null”更改为“empty”,试图打印出 CandidateChoice.getName() 并且发生了同样的事情 - 没有打印任何内容并循环返回菜单。但是当我输入一个有效的名称时,该方法会按预期工作。
  • 好的。现在也许,向您的构造函数添加一条消息,上面写着“使用以下值创建候选人:等等。”以查看您的“空”候选人是否真的被创建并缩小搜索区域

标签: java arrays validation search input


【解决方案1】:

正如我们在 cmets 中一起看到的,问题在于您根本没有使用方法的返回值来打印它。

而在你输入正确的候选人姓名时它会打印一些东西的原因是因为你使用了 display() 方法,其中包含一个打印方法。

if(candidateChoice.getName().equals("null"))
          {
              return "There are no candidates with this name.";
          }
return candidateChoice.display();

正确的解决方案是:

  • 调整显示方法以在Candidate 为空时显示内容

或者

  • 创建一个Candidate 的新方法,它只返回一个字符串,而不显示它包含有关候选人的信息,并对空候选人进行特殊测试,您可以在这里使用

新代码:

public static String displayCandidate(Candidate[] candidate)
{
    System.out.println("Please enter the name of the candidate (LAST,FIRST)");                                      
    Candidate candidateChoice = searchCandidate(candidate);
    return candidateChoice.getInformation();
}

getInformation() 方法类似于:

public String getInformation(){
    if (this.getName().equals("null"){
        return "Candidate does not exist";
    }else{
        // Here belongs the information you want to display from the Candidate object
    }
}

【讨论】:

    【解决方案2】:

    我看到您正在尝试实现二进制搜索之类的东西,但这是在重新发明轮子。而你就可以做到这一点。

      public static Candidate searchCandidate(Candidate[] candidate)
        {
        Scanner kbd = new Scanner(System.in);
                String searchKey = kbd.nextLine();
                searchKey = searchKey.toUpperCase();
        int search = Arrays.binarySearch(candidate, new Candidate(searchkey,"null","null","null"), new Comparator<Candidate>(){
            @Override
            public int compare(Candidate s1, Candidate s2) {
              return s1.getName().compareTo(s2.getName());
            }
         });
    
           if (search!=-1) 
              return candidate[search];
           else
              return Candidate(null,"null","null","null");
    
          }
    

    【讨论】:

    • 我很欣赏您的解决方案,但是我拥有的二进制搜索是项目的要求。我们还必须使用手动冒泡排序方法,因此在我们可以使用的工具方面存在一些限制。
    • 那么我很遗憾地告诉你,你正在以完全错误的方式实现 binarySearch 算法。
    • 这并没有真正的帮助,也没有带来解决方案。
    猜你喜欢
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多