?

missing return statement!


hi guys,

Im trying to compile this class below, but its just not working, it keeps sating 'missing return statement' int the method declaration line. Can u see to check if there is anything missing in my code or if iv done something wrong??

Thanks!

public class swapping {

public int trade(int []a, int b){ //says theres a missing return statement 
int temp=0;
for (int n = 1; n < b; n++) {
for (int m = 0; m < b - 1; m++) {
if (a[m] > a[m] + 1) { 
temp = a[m];
a[m] = a[m+1];
a[m+1] = temp;
return a[m]; 
}
}
}
}empty program!
}

Recommended Answers

All 10 Replies

?

missing return statement!


hi guys,

Im trying to compile this class below, but its just not working, it keeps sating 'missing return statement' int the method declaration line. Can u see to check if there is anything missing in my code or if iv done something wrong??

Thanks!


public class swapping {

public int trade(int []a, int b){ //says theres a missing return statement
int temp=0;
for (int n = 1; n < b; n++) {
for (int m = 0; m < b - 1; m++) {
if (a[m] > a[m] + 1) {
temp = a[m];
a[m] = a[m+1];
a[m+1] = temp;
return a[m];
}
}
}
}empty program!
}

You have the return in the 'if'. What happens if the 'if' doesn't execute. The method doesn't have any other return statement. It will not return if it doesn't go in the 'if'. You must have a return for all the cases, if you have lots of 'if' statements:

if () {
  ...
  return ;
} else if () {
  ...
  return ;
}
// if both the above are false, no if will be executed so no 'return ;' , you need to add one more outside the ifs and any other whiles, loops switches, ...
return ;

i agree to it so all you have to do is

      public class swapping {

      public int trade(int []a, int b)
{ //says theres a missing return statement
      int temp=0;
      for (int n = 1; n < b; n++) 
      {
            for (int m = 0; m < b - 1; m++) 
            {
              if (a[m] > a[m] + 1) 
              {
                temp = a[m];
                a[m] = a[m+1];
                a[m+1] = temp;
                return a[m];
              }
            else
             return a[m];
            }//end of inner loop
          }//end of outer loop
      }//End of Function
      }//End of Class

i agree to it so all you have to do is

      public class swapping {

      public int trade(int []a, int b)
{ //says theres a missing return statement
      int temp=0;
      for (int n = 1; n < b; n++) 
      {
            for (int m = 0; m < b - 1; m++) 
            {
              if (a[m] > a[m] + 1) 
              {
                temp = a[m];
                a[m] = a[m+1];
                a[m+1] = temp;
                return a[m];
              }
            else
             return a[m];
            }//end of inner loop
          }//end of outer loop
      }//End of Function
      }//End of Class

I think it would be wrong where you put the return statement. With that way, the method will return at the very first for loop iteration. Because the if the first time would normally be false, so it will immediately return.
And I don't know if it will compile, because the return is inside the for loop. In general, it is not certain that the execution will enter the loop so the return might not execute.

You must put the return outside the for loops. And have it return what your method is suppose to do in the case that the if or in general the loop does not execute.

The problem is that, you haveused the if statement , you have mentioned the return statement when tour if statement is satisfied but what will the program return when if condition is false.
So, use the else statement with proper return in it.

I agree with Dupron

commented: Resurrecting a thread and nothing valuable to add. -3

why isnt this post closed???? I do believe it was concluded, unless the poster believes in showing the full code???

because usually, "closing" threads isn't necessary. the dates of the posts can tell whether or not the thread is still active.
chances are, that if you read a thread that's not on the first (two?) pages, that the thread can be considered 'ancient', and thus, dead.

Why not flag that it was solved????

sometimes a dead thread isn't solved.
sometimes those asking questions don't bother, forget, ...

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.