| | |
Compiler says: "missing return statement"...even though I have a return statement!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 0
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.
Does anybody know what is wrong?
java Syntax (Toggle Plain Text)
public static int nameSearch(String[] inOrder, String searchedFor) { for (int i = 0; i < inOrder.length; i++) { if (searchedFor == inOrder[i]) { return i; } else if (i > inOrder.length) { return -1; } } }
Does anybody know what is wrong?
Last edited by CheeseDonkey; Apr 20th, 2008 at 9:29 pm.
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
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:
hope this helps ..
like this:
Java Syntax (Toggle Plain Text)
public static int nameSearch(String[] inOrder, String searchedFor){ int returnVal = null; for (int i = 0; i < inOrder.length; i++){ if (searchedFor == inOrder[i]){ returnVal = i; } else if (i > inOrder.length){ returnVal = -1; } } return returnVal; //return here... }
hope this helps ..
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.
P.S. You can't set an int to null anyway. ;-)
And, don't use == to compare Strings. ;-)
Java Syntax (Toggle Plain Text)
public static int nameSearch(String[] inOrder, String searchedFor){ int returnVal = -1; if (searchedFor == null) { // if searchedFor is null the if in the for loop will have problems. return returnVal; } for (int i = 0; i < inOrder.length; i++) { if (searchedFor.equals(inOrder[i])) { returnVal = i; } } return returnVal; }
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
----------------------------------------------
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
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
Re: Compiler says: "missing return statement"...even though I have a return statement!
0
#4 Apr 21st, 2008
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.
Re: Compiler says: "missing return statement"...even though I have a return statement!
0
#6 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.
i.e.
Java Syntax (Toggle Plain Text)
public String aMethod() { int x = 0; //do something with x if (x == 0) { return "Hello"; } }
i.e.
Java Syntax (Toggle Plain Text)
public String aMethod() { int x = 0; //do something with x if (x == 0) { return "Hello"; } else if (x == 1) { return "GoodBye"; } else { return "Bogus"; } } // or public String aMethod() { int x = 0; //do something with x if (x == 0) { return "Hello"; } return "Bogus"; } // or public String aMethod() { int x = 0; //do something with x if (x <= 0) { return "Hello"; } else if (x > 0) { // the same as a simple else, in this case return "GoodBye"; } }
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
----------------------------------------------
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
Re: Compiler says: "missing return statement"...even though I have a return statement!
0
#7 Apr 21st, 2008
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
----------------------------------------------
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
Re: Compiler says: "missing return statement"...even though I have a return statement
0
#9 Apr 21st, 2008
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
Re: Compiler says: "missing return statement"...even though I have a return statement
0
#10 Apr 21st, 2008
also this code is wrong
![]() |
Other Threads in the Java Forum
- Previous Thread: Finding whitespaces
- Next Thread: Counting Letters in a String
Views: 2053 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie number object objects oracle oriented panel print printf problem program programming project projects recursion replaydirector reporting researchinmotion return robot rotatetext scanner screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows







