Results 1 to 9 of 9
  1. #1
    Aenohe is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default 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.");
            }
        }

  2. #2
    zirbinator is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default 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;
    	    }
    }
    If you don't want the method to return a boolean value at all, just change boolean in the method declaration to "void" and don't add the return statements that I put in.

  3. #3
    Aenohe is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: Missing return statement

    Thanks a bunch.

    Feel a bit embarrassed i thought that it is enough to have
    Java Code:
     found = true;
    as statement.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Missing return statement

    VBA does that - not sure if VB has the same syntax, but it certainly confused the hell out of me.

  6. #6
    Aenohe is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default 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.

  7. #7
    vanhongin143 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default 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

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default 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(){
    That says the method PowerFunc (which should be powerFunc according to naming standards) returns a String.
    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 **

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default 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

  1. Missing Return statement =[
    By avirunes in forum New To Java
    Replies: 6
    Last Post: 02-12-2011, 10:34 AM
  2. Missing return statement
    By gkoef in forum New To Java
    Replies: 8
    Last Post: 01-01-2011, 02:52 AM
  3. Missing return statement error.
    By Fortu in forum New To Java
    Replies: 2
    Last Post: 12-11-2010, 09:15 PM
  4. missing return statement
    By bayan in forum New To Java
    Replies: 6
    Last Post: 04-26-2010, 03:15 PM
  5. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •