Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys
i'm new to java code and i'm having trouble with this method i wrote :
Java
/** BinarySearchRecursive
	 * 
	 * @param name = to search
	 * @param l = left index
	 * @param r = right index
	 * @param m = middle index
	 */
	public int Binary_search( String name, int l , int r )
	{
		int lenForSearch = name.length();
		
		if( name.compareToIgnoreCase(AddBook.get(l).get_fname().substring(0,lenForSearch)) == 0 )//found contact's index
		{
			return l;
		}
		else if( l == r )return -1;//contact was not found
		
		int  m = ( (l+r)/2 );
		
		if( name.compareToIgnoreCase(AddBook.get(m).get_fname().substring(0,lenForSearch)) > 0 )//name < AddBook[m]
		{
			 return Binary_search( name, l , m );
		}
		
		else Binary_search( name, m , r );//name > AddBook[m]
	}

the error is about a return statements issue and i can't find where is the problem.
the ERROR " This method must return a result of type int "
please guide me (:
Posted
Updated 17-Apr-14 0:26am
v3
Comments
Salman622 17-Apr-14 7:01am    
try debugging the value returning is not of type integer

1 solution

Probably this will work for you:
Java
/** BinarySearchRecursive
     *
     * @param name = to search
     * @param l = left index
     * @param r = right index
     * @param m = middle index
     */
    public int Binary_search( String name, int l , int r )
    {
        int lenForSearch = name.length();

        if( name.compareToIgnoreCase(AddBook.get(l).get_fname().substring(0,lenForSearch)) == 0 )//found contact's index
        {
            return l;
        }
        else if( l == r )return -1;//contact was not found

        int  m = ( (l+r)/2 );

        if( name.compareToIgnoreCase(AddBook.get(m).get_fname().substring(0,lenForSearch)) > 0 )//name < AddBook[m]
        {
             return Binary_search( name, l , m );
        }

        //here you have missed the return statement
        else return Binary_search( name, m , r );//name > AddBook[m]
    }


You've missed the return statement in the last case. In every case the code has to return an integer value if you set an integer to the return type for your function.
 
Share this answer
 
Comments
idobry 17-Apr-14 7:01am    
Thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900