【问题标题】:Searching an array list for a specific record in java在java中搜索特定记录的数组列表
【发布时间】:2012-10-17 15:30:00
【问题描述】:

我正在编写一个方法来返回数组中的特定记录,但是它会引发两个错误,我不知道如何修复它。谁能解释我做错了什么?

public String find(String searchName) 
{ // ERROR - MISSING RETURN STATEMENT
    Iterator<TelEntry> iterator = Directory.entries.iterator();
    boolean hasFound = false;
    while (iterator.hasNext()) 
    {
        TelEntry entry = iterator.next();

        if (entry.name.equalsIgnoreCase(searchName)) {
            return entry.name + entry.telNo;
            hasFound = true; // ERROR UNREACHABLE STATEMENT
        }

    }
    if (hasFound==false)
    {
        System.out.println("sorry, there is noone by that name in the Directory. Check your spelling and try again");
    }
}

谁能解释我做错了什么?

【问题讨论】:

  • if (!hasFound) throw new NoSuchElementException(); (并使用@throws 子句将其记录在方法文档中)

标签: java arraylist return unreachable-statement


【解决方案1】:

您遇到的基本问题是,当找不到匹配项时,您没有返回语句。通常,在这种情况下,方法会返回null,但您可能希望返回searchName,甚至是错误消息——这取决于方法的意图/合同是什么(未说明)。

然而,你遇到的另一个问题是你的代码对于它正在做的事情来说太复杂了,尤其是 hasFound 变量完全没用。

把你的代码改成这样,它做的事情完全一样,但表达得更优雅:

public String find(String searchName) {
    for (TelEntry entry : Directory.entries) {
        if (entry.name.equalsIgnoreCase(searchName)) {
            return entry.name + entry.telNo;
        }
    }
    System.out.println("sorry, there is noone by that name in the Directory. Check your spelling and try again");
    return null; // or return "searchName", the error message, or something else
}

【讨论】:

    【解决方案2】:

    return 语句应该是块中的最后一个语句。在下面更改您的代码:

    if (entry.name.equalsIgnoreCase(searchName)) {
                hasFound = true; // ERROR UNREACHABLE STATEMENT
                return entry.name + entry.telNo;
            }
    

    【讨论】:

    • +1 我刚开始打字,有新的答案通知:)
    • 如果你真的看懂了代码,你会发现hasFound这个变量在方法中没有什么用处。
    • @Nambari 这将是最好的建议。另外,请注意,即使您修复了这一行,该方法也不会编译,因为在循环之后您必须返回一个String,而您的答案并不能解决这个问题。
    • SO 社区仍然让我感到困惑。答案是否应该涵盖提出的问题?为 OP 写一个新程序?
    • 您是否至少在某处复制/粘贴 OP 代码并测试您的答案是否使代码编译?
    【解决方案3】:

    如果一个方法被声明为返回一个字符串,它必须返回一个字符串,或者抛出一个异常。不返回任何东西是不可接受的。所以你应该决定当找不到字符串时该怎么做。你基本上有两种选择:

    • 返回空值
    • 抛出异常

    打印错误不是一个好主意。这种方法不应该处理用户界面。那不是它的责任,一个方法应该只有一个责任。返回带有错误消息的字符串也不是一个好主意:调用者无法知道返回的字符串是已找到的字符串还是错误字符串。

    此外,您的代码过于复杂。它可以简化为以下内容(假设 Directory.entries() 实现了 Iterable,因为它应该):

    public String find(String searchName) {
        for (TelEntry entry: Directory.entries()) {
            if (entry.name.equalsIgnoreCase(searchName)) {
                 return entry.name + entry.telNo;
            }
        }
        return null;
    }
    

    我会改变返回类型,所以,让它返回一个TelEntry 实例。让调用者处理连接。也不是这个方法的责任。

    【讨论】:

    • @the OP:无论您选择什么,请记住在方法文档中明确说明。
    【解决方案4】:

    你的代码不正确:你不能在同一个块中返回后有指令:它怎么能被执行,因为函数已经返回......?


    这就是编译器告诉你的:unreachable statement

    【讨论】:

      【解决方案5】:

      最好返回字符串本身。确实,直接打印到屏幕上违反了SRP(单一职责原则,也避免按照方法期望返回)。

      不需要布尔检查器。

      public String find(String searchName) { 
              Iterator<TelEntry> iterator = Directory.entries.iterator();
              while (iterator.hasNext()) {
                  TelEntry entry = iterator.next();
                  if (entry.name.equalsIgnoreCase(searchName)) {
                      return entry.name + entry.telNo;
                  }
              }
              return "sorry, there is noone by that name in the Directory. Check your spelling and try again";
      }
      

      【讨论】:

      • 提供的代码不会编译:unreachable 语句。在发布之前测试提供的代码。
      • 2秒,我编译一下,我刚用记事本写的;)
      • 这违反了最小意外原则,并且可能是方法的文档,这要糟糕得多。
      • @Luiggi Mendoza 完成了。你是对的,对于“之前测试它”;)
      【解决方案6】:

      谁能解释我做错了什么?

      我的解释在下面的cmets中:

      public String find(String searchName) 
          { // ERROR - MISSING RETURN STATEMENT
              Iterator<TelEntry> iterator = Directory.entries.iterator();
              boolean hasFound = false;
              while (iterator.hasNext()) 
              {
                  TelEntry entry = iterator.next();
      
                  if (entry.name.equalsIgnoreCase(searchName)) {
                      return entry.name + entry.telNo;
                      /*The "return" statement above stops executing
                      the current method and transfers control to the 
                      place from where "find" method was called. The
                      statement below this line is NEVER executed.*/
                      hasFound = true; // ERROR UNREACHABLE STATEMENT
                  }
      
              }
              if (hasFound==false)
              {
                  System.out.println("sorry, there is noone by that name in the Directory. Check your spelling and try again");
              }
              /*This method starts with "public String". That means that 
              it MUST return a String object or null when the method 
              finishes executing.
              The best place to return is here.*/
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多