Compiler says: "missing return statement"...even though I have a return statement!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 1
Reputation: CheeseDonkey is an unknown quantity at this point 
Solved Threads: 0
CheeseDonkey CheeseDonkey is offline Offline
Newbie Poster

Compiler says: "this method must return a result of type int". That's what I'm doing!

 
0
  #1
Apr 20th, 2008
This is making me angry. I keep getting a "this method must return a result of type int" error message, but obviously I have a return statement. This is a program that is supposed to perform a sequential search for a string index.

  1. public static int nameSearch(String[] inOrder, String searchedFor)
  2. {
  3. for (int i = 0; i < inOrder.length; i++)
  4. {
  5. if (searchedFor == inOrder[i])
  6. {
  7. return i;
  8. }
  9. else if (i > inOrder.length)
  10. {
  11. return -1;
  12. }
  13. }
  14. }

Does anybody know what is wrong?
Last edited by CheeseDonkey; Apr 20th, 2008 at 9:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: ikugan26 is an unknown quantity at this point 
Solved Threads: 0
ikugan26 ikugan26 is offline Offline
Newbie Poster

Re: Compiler says: "missing return statement"...even though I have a return statement

 
0
  #2
Apr 20th, 2008
Dont use return statement inside if statements, instead use it at the end of your method name search
like this:
  1. public static int nameSearch(String[] inOrder, String searchedFor){
  2. int returnVal = null;
  3. for (int i = 0; i < inOrder.length; i++){
  4. if (searchedFor == inOrder[i]){
  5. returnVal = i;
  6. }
  7. else if (i > inOrder.length){
  8. returnVal = -1;
  9. }
  10. }
  11. return returnVal; //return here...
  12. }

hope this helps ..
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Compiler says: "missing return statement"...even though I have a return statement!

 
0
  #3
Apr 21st, 2008
Of course, that second part of the if statement is completely false anyway. That is what the for loop is for.

  1. public static int nameSearch(String[] inOrder, String searchedFor){
  2. int returnVal = -1;
  3. if (searchedFor == null) {
  4. // if searchedFor is null the if in the for loop will have problems.
  5. return returnVal;
  6. }
  7. for (int i = 0; i < inOrder.length; i++) {
  8. if (searchedFor.equals(inOrder[i])) {
  9. returnVal = i;
  10. }
  11. }
  12. return returnVal;
  13. }

P.S. You can't set an int to null anyway. ;-)

And, don't use == to compare Strings. ;-)
Last edited by masijade; Apr 21st, 2008 at 1:02 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: ikugan26 is an unknown quantity at this point 
Solved Threads: 0
ikugan26 ikugan26 is offline Offline
Newbie Poster

Re: Compiler says: "missing return statement"...even though I have a return statement!

 
0
  #4
Apr 21st, 2008
P.S. You can't set an int to null anyway. ;-)
oops! sorry.. and thanks..
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Compiler says: "missing return statement"...even though I have a return statement!

 
0
  #5
Apr 21st, 2008
It's perfectly fine to have return statements within if() blocks. The issue here is that there is not a guaranteed return path from the method. For instance, in the original code if inOrder.length is 0, there is no return. The compiler requires a valid return path regardless of any conditional statements and that is why you could not compile it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Compiler says: "missing return statement"...even though I have a return statement!

 
0
  #6
Apr 21st, 2008
Originally Posted by Ezzaral View Post
It's perfectly fine to have return statements within if() blocks. The issue here is that there is not a guaranteed return path from the method. For instance, in the original code if inOrder.length is 0, there is no return. The compiler requires a valid return path regardless of any conditional statements and that is why you could not compile it.
Yes, you can return from an if. However, if the only return statement you have is contained within an if statement, and the compiler cannot confirm that every possible avenue has been covered, you will still get a "missing return statement".

i.e.
  1. public String aMethod() {
  2. int x = 0;
  3. //do something with x
  4. if (x == 0) {
  5. return "Hello";
  6. }
  7. }
will cause the compiler to throw that error, as the compiler cannot confirm that something will be returned. If you are going to return from an if statement, then return from every block in the if statement and include an else that also has a return statement or have a return statement at the end of the method.

i.e.
  1. public String aMethod() {
  2. int x = 0;
  3. //do something with x
  4. if (x == 0) {
  5. return "Hello";
  6. } else if (x == 1) {
  7. return "GoodBye";
  8. } else {
  9. return "Bogus";
  10. }
  11. }
  12. // or
  13. public String aMethod() {
  14. int x = 0;
  15. //do something with x
  16. if (x == 0) {
  17. return "Hello";
  18. }
  19. return "Bogus";
  20. }
  21. // or
  22. public String aMethod() {
  23. int x = 0;
  24. //do something with x
  25. if (x <= 0) {
  26. return "Hello";
  27. } else if (x > 0) { // the same as a simple else, in this case
  28. return "GoodBye";
  29. }
  30. }

In other words, every possible branch must be covered in such a way that the compiler can determine that it is covered.
Last edited by masijade; Apr 21st, 2008 at 2:29 pm.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Compiler says: "missing return statement"...even though I have a return statement!

 
0
  #7
Apr 21st, 2008
That is exactly what I was trying to say
Perhaps the language wasn't clear.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Compiler says: "missing return statement"...even though I have a return statement!

 
0
  #8
Apr 21st, 2008
Just wanted to make sure the OP understood. ;-)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Compiler says: "missing return statement"...even though I have a return statement

 
0
  #9
Apr 21st, 2008
Ah, ok. I thought maybe what I wrote, while seeming clear to me, was far from it to anyone else
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Compiler says: "missing return statement"...even though I have a return statement

 
0
  #10
Apr 21st, 2008
Originally Posted by ikugan26 View Post
  1. int returnVal = null;
also this code is wrong
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 2053 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC