1

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

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

谁能解释我做错了什么?

4

6 回答 6

6

您遇到的基本问题是,当找不到匹配项时,您没有返回语句。通常,在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
}
于 2012-10-27T18:01:11.373 回答
2

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

if (entry.name.equalsIgnoreCase(searchName)) {
            hasFound = true; // ERROR UNREACHABLE STATEMENT
            return entry.name + entry.telNo;
        }
于 2012-10-27T17:50:38.030 回答
2

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

  • 返回空
  • 抛出异常

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

此外,您的代码过于复杂。它可以简化为以下内容(假设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实例。让调用者处理连接。这也不是这个方法的责任。

于 2012-10-27T18:04:17.067 回答
1

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


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

于 2012-10-28T10:34:19.207 回答
0

最好返回字符串本身。确实,直接将其打印到屏幕上违反了 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";
}
于 2012-10-27T17:53:04.593 回答
0

谁能解释我做错了什么?

我的解释在下面的评论中:

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.*/
    }
于 2012-10-27T18:12:53.073 回答