954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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.

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?

CheeseDonkey
Newbie Poster
1 post since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Dont use return statement inside if statements, instead use it at the end of your method name search
like this:

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 .. ;)

ikugan26
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Of course, that second part of the if statement is completely false anyway. That is what the for loop is for.

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. ;-)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
P.S. You can't set an int to null anyway. ;-)


oops! sorry.. and thanks..

ikugan26
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
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.

public String aMethod() {
  int x = 0;
  //do something with x
  if (x == 0) {
    return "Hello";
  }
}

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.

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 branchmust be covered in such a way that the compiler can determine that it is covered.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

That is exactly what I was trying to say :)
Perhaps the language wasn't clear.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Just wanted to make sure the OP understood. ;-)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Ah, ok. I thought maybe what I wrote, while seeming clear to me, was far from it to anyone else :P

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
int returnVal = null;


also this code is wrong

bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 

import java.io.*;
class Block
{
int a,b,c,volume;
Block(int x,int y,int z)
{
a=x;
b=y;
c=z;
volume=a*b*c;
}
boolean sameBlock(Block ob)
{
if((ob.a==a)&(ob.b==b)&(ob.c==c))
return true;
else
return false;
}
boolean sameVolume(Bolck ob)
{
if(ob.volume==volume)
return true;
else
return false;
}
}
public class DemoOb
{
public static void main(String args[])
{
Block ob1=new Block(10,5,2);
Block ob2=new Block(10,2,5);
System.out.println("the dimension of ob1 is same as ob2" + ob1.sameBlock(ob2));
System.out.println("the volume of ob1 is same as ob2" + ob1.sameVolume(ob2));
}
}


:19: cannot find symbol

papay0011
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
import java.io.*;
class Block
{
int a,b,c,volume;
 Block(int x,int y,int z)
{
a=x;
b=y;
c=z;
volume=a*b*c;
}
boolean sameBlock(Block ob)
{
if((ob.a==a)&(ob.b==b)&(ob.c==c))
return true;
else 
return false;
}
boolean sameVolume(Bolck ob)
{
if(ob.volume==volume)
return true;
else
return false;
}
}
public class DemoOb
{
public static void main(String args[])
{
Block ob1=new Block(10,5,2);
Block ob2=new Block(10,2,5);
System.out.println("the dimension of ob1 is same as ob2" + ob1.sameBlock(ob2));
System.out.println("the volume of ob1 is same as ob2" + ob1.sameVolume(ob2));
}
}

:19:cannot find symbol at boolean sameVolume(Block ob)

papay0011
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Bolck ???

JamesCherrill
Posting Genius
Moderator
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

thanks JamesCherrill

papay0011
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
public class apples
{
public static void main(String[] args)
{
System.out.println(Average(12,34,45,5,6,7,8,99,1,111,23,51,3));

public static int Average(int ...v)
{
int total=0;
for(int x:v)
total +=x;
return total/v.length;
}
}
}


error....

papay0011
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

If you indent the code properly you will see what's wrong - it's all to do with the {}

JamesCherrill
Posting Genius
Moderator
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You