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?

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

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

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.

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

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

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)

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

This article has been dead for over six months. Start a new discussion instead.