【发布时间】: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