Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By JosAH

Thread: boolean method return statement error

  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default boolean method return statement error

    Java Code:
        public boolean goToFirstEmpty(){
            while (seesSlot() && seesCD()){
                moveOn();
                if (!seesCD()){
                    return true;
                }
            }
    When I compile it says that it needs a return statement but it has one? I

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: boolean method return statement error

    Does it return anything when it doesn't see a Slot or doesn't see a CD to start with?

    kind regards,

    Jos
    quad64bit likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: boolean method return statement error

    Does it return the boolean value of true?
    Write a Finder subclass of Vic with two methods: goToFirstEmpty
    and goToFirstFilled, where the executor advances until it comes to the first empty
    slot or the first filled slot, respectively, or to where seesSlot() is false if necessary.

    The question doesn't tell me to return a value or anything...

  4. #4
    Tombomb is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: boolean method return statement error

    Are you getting a compile error or run time?

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: boolean method return statement error

    compile error

  6. #6
    Tombomb is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: boolean method return statement error

    It is because there is no guaranteed return statement because its possible the if condition will never happen. Add in another return after the if statement and it should go away.

  7. #7
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: boolean method return statement error

    Quote Originally Posted by Tombomb View Post
    It is because there is no guaranteed return statement because its possible the if condition will never happen. Add in another return after the if statement and it should go away.
    Doy, thanks. I should have thought of that.

  8. #8
    Tombomb is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: boolean method return statement error

    No problem :)

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: boolean method return statement error

    Quote Originally Posted by Army View Post
    Does it return the boolean value of true?
    Java is kind of dull: when you don't tell it to return anything it won't return anything; suppose that while-loop fails; nothing will be returned and that's what the compiler complains about.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. method return statement is wrong
    By ftrengnr in forum New To Java
    Replies: 1
    Last Post: 10-19-2010, 04:15 AM
  2. Return statement error
    By Exothesis in forum New To Java
    Replies: 2
    Last Post: 10-13-2010, 01:56 AM
  3. Return Statement and Boolean Help
    By GhostShaman in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 11:15 AM
  4. Replies: 5
    Last Post: 01-16-2010, 11:14 PM
  5. Replies: 18
    Last Post: 12-02-2009, 12:01 PM

Posting Permissions

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