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

P.S. You can't set an int to null anyway. ;-)

oops! sorry.. and thanks..

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.

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

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

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

int returnVal = null;

also this code is wrong

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)

Bolck ???

thanks JamesCherrill

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.