在java中搜索特定记录的数组列表

时间:2012-10-27 17:49:48

标签: java arraylist return unreachable-statement

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

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

谁能解释我做错了什么?

6 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:2)

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

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

答案 2 :(得分:2)

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

  • 返回null
  • 抛出异常

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

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

答案 3 :(得分:1)

您的代码不正确:在同一个块中返回后您无法获得指令:如何执行,因为函数已返回...?


这就是编译器告诉你的:无法访问的语句

答案 4 :(得分: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";
}

答案 5 :(得分: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.*/
    }