Results 1 to 10 of 10
  1. #1
    daniellex0x is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default Missing a return statment??? (toString)

    It keeps telling me I'm missing a return statment... but I'm not! I don't think...

    public String toString () {
    if (this.permit = true)
    return ("Car " + this.plateNumber + "has a permit");
    else if (this.permit = false)
    return("Car " + this.plateNumber);

    -> }


    The "arrow" (->) is where it's saying I need one....I don't get it :S

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    = and == are very different things. Read up on the difference and decide which you mean in this context.

    http://download.oracle.com/javase/tu...bolts/op1.html and following.
    Last edited by pbrockway2; 10-18-2010 at 01:07 AM.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Also, your return statements are enclosed in if and else if statements, if both evaluate to false, there is no return statement in the code, that's why the compiler is complaining. Even though your two statements can never both evaluate to false, the compiler doesn't know that.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  4. #4
    daniellex0x is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    I mean, that if this.permit is true then I want it to do that output, and if it's false, I want it to do the other.

    I tried changing it to "==" but it still the same error

  5. #5
    daniellex0x is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    sorry, but how would I change that?

  6. #6
    daniellex0x is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    Ohhh!!! I get it!! Thank you :)

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Generally we write

    Java Code:
    if(foo == true) {
      // whatever
    }

    as

    Java Code:
    if(foo) {
        // whatever
    }

    Likewise "if(foo == false) {" is better expressed as "if(!foo) {".

    If one branch or other is supposed to be taken: that's what we use "else" for. So instead of:

    Java Code:
    if(foo) {
        // do one thing
    }
    if(!foo) {
        // do something else
    }

    we would just say

    Java Code:
    if(foo) {
        // do one thing
    } else {
        // do something else
    }

    -----

    The compiler is quite conservative in how it deals with if statements and the possible places that it expects you could return from (as M00nchile observes). But it understands the if-else construct well.
    Last edited by pbrockway2; 10-18-2010 at 01:15 AM. Reason: ... slow ;(

  8. #8
    karanhs6 is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    if its a string the use class.equals(blahclass)
    and if its int did u change both
    and if tht doesnt work try goin the other way !=

  9. #9
    daniellex0x is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    I did
    public String toString () {
    if (this.permit == true)
    return ("Car " + this.plateNumber + "has a permit");
    else return("Car " + this.plateNumber);

    }

    and it worked :) (for some reason or other)

  10. #10
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Also, as pbrockway2 explained, you can ditch the == true in your if statement.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. missing return statement
    By bayan in forum New To Java
    Replies: 6
    Last Post: 04-26-2010, 03:15 PM
  2. Missing Return Statement Error
    By darkblue24 in forum New To Java
    Replies: 13
    Last Post: 02-16-2010, 08:22 PM
  3. Replies: 5
    Last Post: 01-16-2010, 11:14 PM
  4. Replies: 18
    Last Post: 12-02-2009, 12:01 PM
  5. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM

Tags for this Thread

Posting Permissions

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