Results 1 to 5 of 5
  1. #1
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Angry error: missing return statement

    Hi,

    Just finished writing my method for triangle and for some reasons jgrasp can't compile it saying " error: missing return statement" which I don't know how to fix. My class and extends are below:

    Java Code:
    //test isTriangle( )
    		tVal = isTriangle(len1, len2, len3);
    		if(tVal == 3)
    			System.out.println("All three sides are equal");
    		if(tVal == 2)
    			System.out.println("Exactly two sides are equal");
    		if(tVal == 1)
    			System.out.println("No sides are equal");	
    		if(tVal == 0)
    			System.out.println("The values of the sides do not form a triangle");
    METHOD:

    Java Code:
    	public static int isTriangle(int a, int b, int c)
    		{
    		
    		
    	 		if(a>=(b+c)||b>=(a+c)||c>=(a+b))
    				return(0);
    		
    		
    			
    			if(a==b&&a==c&&b==c)
    				return(3);
    		
    		
    				
    			if(a==(b+c)||b==(a+c)||c==(a+b))
    				return(2);
    		
    		
    			
    			if(a!=b&&a!=c&&b!=c)
    				return(1);
    		
    		
    		}

  2. #2
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: error: missing return statement

    jgrasp error:

    HW2_methodLibrary.java:121: error: missing return statement
    } // error pointing the bottom bracket.
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.

  3. #3
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: error: missing return statement

    adding return(-1) fixed the problem. can someone tell me why???

    Java Code:
    public static int isTriangle(int a, int b, int c)
    		{
    		
    		
    	 		if(a>=(b+c)||b>=(a+c)||c>=(a+b)){
    				return(0);
    			}
    		
    			
    			if(a==b&&a==c&&b==c){
    				return(3);
    			}
    		
    				
    			if(a==(b+c)||b==(a+c)||c==(a+b)){
    				return(2);
    		
    			}
    			
    			if(a!=b&&a!=c&&b!=c){
    				return(1);
    			}
    			
    			return(-1);//why -1??
    			
    		}

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: error: missing return statement

    The compiler doesn't know if any of the if statements will be satisfied. If none are satisfied, then there's a risk that the method will end with nothing returned. You must either return a default value at the end or use an else block so that it is impossible for a non-return situation from happening.

  5. #5
    serotonin is offline Member
    Join Date
    Jan 2013
    Location
    england
    Posts
    9
    Rep Power
    0

    Default Re: error: missing return statement

    is it that the compiler reads all your return statements inside 'if' control bodies,
    so it needs to find a return statement which will be accessible without the 'if' expressions being met,
    so basically... you have to make sure a return statement will be reached.

Similar Threads

  1. Missing return statement error
    By Asglv in forum New To Java
    Replies: 1
    Last Post: 01-13-2013, 09:28 AM
  2. missing return statement error
    By Joshkesner in forum New To Java
    Replies: 2
    Last Post: 10-21-2012, 06:38 AM
  3. Missing return statement error.
    By Fortu in forum New To Java
    Replies: 2
    Last Post: 12-11-2010, 09:15 PM
  4. Missing Return Statement Error
    By darkblue24 in forum New To Java
    Replies: 13
    Last Post: 02-16-2010, 08:22 PM
  5. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •