Results 1 to 9 of 9
Thread: Missing return statement
- 11-18-2011, 02:57 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
Missing return statement
Cant find mistake in this code - can some1 help - please.
Keep getting "missing return statement" when i try to compile.
Java Code:public boolean search(String searchString){ int index = 0; boolean found = false; while(index < notes.size() && !found) { String note = notes.get(index); if(note.contains(searchString)) { found = true; } else { index++; } } if(found) { System.out.println("Found search term in note: " + notes.get(index)); } else { System.out.println("Search term not found."); } }
- 11-18-2011, 03:08 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
Re: Missing return statement
In the first line of the code, the method declaration, you specified that the return type of the method is "boolean." However, you never return a boolean value (true or false). It looks like you would want to return "true" if the search finds something and "false" if it does not. The code would look like this:
Java Code:public boolean search(String searchString) { int index = 0; boolean found = false; while(index < notes.size() && !found) { String note = notes.get(index); if(note.contains(searchString)) { found = true; } else { index++; } } if(found) { System.out.println("Found search term in note: " + notes.get(index)); return true; } else { System.out.println("Search term not found."); return false; } }
- 11-18-2011, 04:12 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
Re: Missing return statement
Thanks a bunch.
Feel a bit embarrassed i thought that it is enough to haveJava Code:found = true;
- 11-18-2011, 10:45 PM #4
Re: Missing return statement
I vaguely remember seeing some language where you write a statement that looks like you're assigning a value to the name of the method, and that sets the return value. But Java doesn't work that way.
Get in the habit of using standard Java naming conventions!
- 11-18-2011, 10:52 PM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
Re: Missing return statement
VBA does that - not sure if VB has the same syntax, but it certainly confused the hell out of me.
- 11-18-2011, 11:32 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
Re: Missing return statement
That code is actually from the book but they have obviously made mistake which @zirbinator kindly explained to me.
"Objects First with Java" by David Barnes & Michael Kölling 4th edition.
- 03-07-2012, 01:52 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
Re: Missing return statement
import java.util.Scanner;
/**
* Program Name: Name
* Programmer: Jaevan Clarck Y. Tanduyan
* Year & Section: 4-St.Nicholas
* Date Coded: March 5, 2012
*/
public class Power{
public String PowerFunc(){
Scanner van = new Scanner(System.in);
double a,b,power,c,ans;
int exponent;
System.out.print("Enter a number: ");
a = van.nextDouble();
System.out.print("Enter another number: ");
b = van.nextDouble();
c = a+b;
exponent=1;
while (exponent <= 200) {
System.out.println(+exponent+ " ");
exponent++;
}
while (c <= exponent ) {
System.out.println("\nThe power is " +c+ " ");
c++;
}
}
}
i cant understand why its missing return statement
- 03-07-2012, 02:01 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Missing return statement
Please use [code] tags [/code] when posting code.
Please don't resurrect old threads. You can provide a link to a thread you think is relevant in your thread.
Java Code:public String PowerFunc(){
As it stands that method does not return a String...or anything for that matter.Please do not ask for code as refusal often offends.
** This space for rent **
- 03-07-2012, 02:03 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Re: Missing return statement
vanhongin143, even the question is quite similar to the original not related. Next time, please start a new thread in relevant sub-forum.
Similar Threads
-
Missing Return statement =[
By avirunes in forum New To JavaReplies: 6Last Post: 02-12-2011, 11:34 AM -
Missing return statement
By gkoef in forum New To JavaReplies: 8Last Post: 01-01-2011, 03:52 AM -
Missing return statement error.
By Fortu in forum New To JavaReplies: 2Last Post: 12-11-2010, 10:15 PM -
missing return statement
By bayan in forum New To JavaReplies: 6Last Post: 04-26-2010, 04:15 PM -
Missing Return Statement error
By anilanar in forum New To JavaReplies: 2Last Post: 08-20-2009, 02:02 AM
Bookmarks