Getting Return Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ALi Shaikh
    New Member
    • Sep 2007
    • 18

    Getting Return Error

    Hello, Whenever I run this Program it says "Return statement missing"
    Code:
    public class Compare3 {
    
       public static Comparable largest (Comparable ob1,Comparable ob2,Comparable ob3)
        {
        	
        	if(ob1.compareTo(ob2)>0)
        	{
        		if(ob1.compareTo(ob3)>0)
        			return ob1;
        		else if(ob1.compareTo(ob3)<1)
        			return ob3;
       		}
       		else 
       			return ob2;
        		
        	
        	
        }
            
       
        
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You don't want to run this program but you want to compile it and the compiler is complaining. You should've included its error diagnostic message verbatim (including the line number where the error was diagnosed).

    The compiler is complaining because of this line:
    Code:
    else if(ob1.compareTo(ob3)<1)
    ... because it isn't clever enough to see that if an integer is not > 0 that it is supposed to be < 1; according to the compiler that test might fail and then nothing else is returned from your method. That test is superfluous so remove it an just leave in the 'else' part.

    kind regards,

    Jos

    Comment

    • hsn
      New Member
      • Sep 2007
      • 237

      #3
      there must be a return value outside the if statement. so if the program didn't enter the if statement there is a return .

      regards
      hsn

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by hsn
        there must be a return value outside the if statement. so if the program didn't enter the if statement there is a return .

        regards
        hsn

        You fell for the lousy indentation trap; there is a return statement at the outermost level.

        kind regards,

        Jos

        Comment

        Working...