Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Boolean return missing

  1. #1
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Boolean return missing

    So I'm trying to check if the new coordinates vs original coordinates are diagonal and 1 line further, and if there is a piece there(getNum()) if it's 2 lines further, so I'm trying to return a boolean value then.

    so if it's the first if, it returns true, if it's the 2nd it returns true, then I say else for all other scenario's, and return false there, but my
    compiler says my method is missing a return statement.

    Where did I go wrong?
    Thx

    public boolean check(int[] d)
        {
            int x,y;
            x = loc[0][0];
            y = loc[0][1];
     
           int sx = d[0];
            int sy = d[1];
     
            if((sx == x+1))
            {
                if((sy == y +1) || (sy == y-1))
                {
                    return true;
     
                }
     
            }
            else if((sx == x+2))
            {
     
                if((buttons[sx-1][sy-1].getNum() == true) || (buttons[sx-1][sy+1].getNum() == true) )
                {
                    return true;
                }
            }
     
     
            else
            {
                return false;
            }
     
        }

    Edit: used a local boolean and returned that after my if's.,
    Last edited by Time4Java; September 25th, 2014 at 08:26 PM.
    English is not my native language (Typo alert).


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Boolean return missing

    Just so you know . . . the compiler doesn't evaluate the if() statements to determine whether there is a logic path that will always result in a return. Rather, it sees that all return statements are contained in if() or if/else logic, and assumes that there may be NO path that results in a return. A way to satisfy the compiler is to always have a default return outside any other logic.

    I don't understand your "local boolean" comment, but you could have simply added a return true or false as the last statement of the method OR evaluated whether the last 'else' condition was required and removed it if not.

  3. #3
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Boolean return missing

    Quote Originally Posted by GregBrannon View Post
    Just so you know . . . the compiler doesn't evaluate the if() statements to determine whether there is a logic path that will always result in a return. Rather, it sees that all return statements are contained in if() or if/else logic, and assumes that there may be NO path that results in a return. A way to satisfy the compiler is to always have a default return outside any other logic.

    I don't understand your "local boolean" comment, but you could have simply added a return true or false as the last statement of the method OR evaluated whether the last 'else' condition was required and removed it if not.
    Yeah, i've learned that now

    By local I mean inside the method: boolean bool = false; then change the returns in the if statements with bool = true and at the end return bool;
    English is not my native language (Typo alert).

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Boolean return missing

    Yes, I know what a local variable is, but I wasn't sure why you needed to create a variable at all. But that's okay. I'm sure it made sense to you at the time.

Similar Threads

  1. missing return statement }
    By longstar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 8th, 2013, 08:37 PM
  2. Missing return statement???
    By Blinktwink in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 21st, 2013, 03:30 AM
  3. missing return statement need help!!
    By widowx in forum What's Wrong With My Code?
    Replies: 32
    Last Post: March 29th, 2013, 01:41 PM
  4. Missing return statement
    By beerye28 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 23rd, 2013, 06:48 PM
  5. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 09:17 AM

Tags for this Thread