Java Array - error saying missing return Statement

method missing return statement error

Page 1 of 1

2 Replies - 7943 Views - Last Post: 25 March 2006 - 02:09 PM Rate Topic: -----

#1 JavaWannaBe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 23-March 06

Java Array - error saying missing return Statement

Posted 25 March 2006 - 12:16 AM

* @param customerName A customer on one of the loans.
* @return The Loan corresponding to the customer. If there is no
* loan for this customer, return null.
*/

public Loan find(String customerName) //***************method getting errors************///
//***************method getting errors************///
{

String compare;
String name = customerName;
//int namelength = name.length();

for (int i = 0; i < loanArray.length; i++)
{
compare = loanArray[i].getCustomerName();
//compare = loanArray[i].substring(0, (namelength)); statement wouldn't work

if (name.equalsIgnoreCase(compare))
{
//rtval = true;
return loanArray[i];
}

else
return null;
}
} ///////////************* I'm getting missing return statement
/////////////////////////////////// error ---here,,,,,WHY???

Is This A Good Question/Topic? 0
  • +

Replies To: Java Array - error saying missing return Statement

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Java Array - error saying missing return Statement

Posted 25 March 2006 - 01:29 PM

Because the for cycle might not execute at all (if the condition is false right at start), and after the for there is no return statement.

Also, I think you have a fault in your program's logic, as your for cycle will only examine the first object of the array (due to the else branch of the if statement).
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Java Array - error saying missing return Statement

Posted 25 March 2006 - 02:09 PM

public Loan find(String customerName)


Per your method statement, you need to return a Loan object.

return loanArray[i];


Here you are returning an array, not a Loan object.

This post has been edited by jayman9: 25 March 2006 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1