Results 1 to 14 of 14
  1. #1
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Post Missing Return Statement Error

    Hi All

    I have problem with below method - I understand it means that Sting is not being retuned - but I don't understand why cause I clearly stated in code that string should be returned. Please help:

    Java Code:
       /**
        * If the argument is between 1 and 3 inclusive,
        * sets the value of its playingMode instance variable to the value of the int argument,
        * otherwise returns the string: "Playing mode unchanged".
        */
       public String selectProgram(int aValue)
       {
           if (aValue >= 1 && aValue <= 3)
           {
               this.setPlayingMode(aValue);
               if (this.getPlayingMode() == 1)
               {
                   return "Repeat off";
                }
                if (this.getPlayingMode() == 2)
                {
                    return "Repeat track";
                }
                if (this.getPlayingMode() == 3)
                {
                    return "Repeat all";
                }
            }
            else 
            {
                return "Playing mode unchanged";
            }
        }  ---- ERROR IS HERE -- It highlights this bracket.

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

    Default

    The if statements are set up so that there is a possibility of the method finishing without returning everything. Go through your logic and you'll see.

  3. #3
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    I really don't understand

    Method should return one String message and its set to do that using if - else statement. Should I use while or for instead?

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

    Default

    Again, it's all in your logic. What if aValue is >= 1 and <=3 but playing mode is not 1, 2, or 3? Even if this is not possible, the compiler doesn't know. You should consider having a default return value or throw an exception.

  5. #5
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    i thought that if 'if' condition is not true than below statement will return String anyway so there is no need for default condition:

    Java Code:
            else 
            {
                return "Playing mode unchanged";
            }
    would changing this.getPlayingMode() for aValue fix the problem?

  6. #6
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    change
    if (this.getPlayingMode() == 3)
    {
    return "Repeat all";
    }
    to
    else{
    return "Repeat all";
    }

    it will solve the problem

  7. #7
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    Thanks!
    That did fixed my problem, but I have exactly the same error (missing return statement) with another method:

    Java Code:
        /**
         * It increments the value of its trackNumber instance variable by 1 
         * but only if its current value is less than the maximum value of 20.
         * Returns the value of the current track number.
         */
        public int up()
        {
        if ((this.trackNumber >= 1) && (this.trackNumber < 20))
        {
            this.setTrackNumber(trackNumber++);
            this.getTrackNumber();
        }
        }

  8. #8
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Like Fubarable said, your inner if clause gives you hard time.

    Run this simple code.
    Use debugger.
    Use values 21,22,23 to see what is going on.
    Remove else in inner if and uncomment last return and it will clear up things for you.

    Java Code:
    public String getInenerIfReponse(int a){
    		
    		if (a >= 22) 
    		{	
    			if (a == 23) //inner if
    				return 	"a = 23";
    			else return "a >= 22 but not 23"; //THIS IS CONDITION YOU MISSED; IF YOU DON'T WRITE THIS LINE, YOU MUST UNCOMMENT : return "inner if does not fulfill condition 
    		}
    		
    		else
    		return "a < 22";
    		
    		// if inner if (a==23) is false, this line will be called to return value
    		//return "inner if does not fulfill condition";
    	}
    got it now?

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

    Default

    Quote Originally Posted by darkblue24 View Post
    Thanks!
    That did fixed my problem, but I have exactly the same error (missing return statement) with another method:

    Java Code:
        /**
         * It increments the value of its trackNumber instance variable by 1 
         * but only if its current value is less than the maximum value of 20.
         * Returns the value of the current track number.
         */
        public int up()
        {
        if ((this.trackNumber >= 1) && (this.trackNumber < 20))
        {
            this.setTrackNumber(trackNumber++);
            this.getTrackNumber();
        }
        }
    Acctually, the problem here is, that there is no return satatement. If all you want to do is increase an inner value, you could just make it's return type void (so, no return statement is necessary), or maybe boolean, just so you know, when trackNumber goes to 20 and won't increment anymore.
    Java Code:
    public boolean up()
        {
        if ((this.trackNumber >= 1) && (this.trackNumber < 20))
        {
            this.setTrackNumber(trackNumber++);
            this.getTrackNumber();
            return true;
        }
        else
        {
           return false;
        }
        }

  10. #10
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    hi Guys

    This is the instructions about how up() method should be constructed:

    When an instance of CDPlayer receives an up() message it should increment
    the value of its trackNumber instance variable by 1 but only if its current value is
    less than the maximum value of 20. The method should then return the value of
    the current track number (whether or not trackNumber has been changed). Write
    the up() method for the CDPlayer class.

    trackNumber instance variable is declared as integer so I think up() method should be returning int (not boolean):

    how can I alter it below code to get rid of 'missing return statement' error ???

    Java Code:
       /**
        * If the argument is between 1 and 3 inclusive,
        * sets the value of its playingMode instance variable to the value of the int argument,
        * otherwise returns the string: "Playing mode unchanged".
        */
       public String selectProgram(int aValue)
       {
           if (aValue >= 1 && aValue <= 3)
           {
               this.setPlayingMode(aValue);
               if (this.getPlayingMode() == 1)
               {
                   return "Repeat off";
                }
                if (this.getPlayingMode() == 2)
                {
                    return "Repeat track";
                }
                if (this.getPlayingMode() == 3)
                {
                    return "Repeat all";
                }
            }
            else 
            {
                return "Playing mode unchanged";
            }
        }  ---- ERROR IS HERE -- It highlights this bracket.

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

    Default

    I'm confused as the answer has already been given, and several times too. Are you looking for someone to re-write your code for you?

  12. #12
    Join Date
    Feb 2010
    Location
    Lino Lakes, MN
    Posts
    4
    Rep Power
    0

    Default

    Your code does not match your instructions, as I see it. It seems to me you haven't even tried to do the work.

  13. #13
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    I just simply could not understood why the error says that int is not returned while it was - some one could explained that to me... I have fixed that method my self:

    Java Code:
        /**
         * It increments the value of its trackNumber instance variable by 1 
         * but only if its current value is less than the maximum value of 20.
         * Returns the value of the current track number.
         */
        public int up()
        {
        if ((this.trackNumber >= 1) && (this.trackNumber < 20))
        {
            this.setTrackNumber(trackNumber++);
            return this.trackNumber;
        }
        else
        {
            return this.trackNumber;
        }
        }
    I found Danderdode's answer rude and pointless. It's this user first post and I hope last on this forum.

    To all the others - Thanks for help guys.

  14. #14
    Join Date
    Feb 2010
    Location
    Lino Lakes, MN
    Posts
    4
    Rep Power
    0

    Default Rude response

    My intent was not to be rude to anyone. I was stating that I didn't understand what the issue was since the description of the question did not match the code, therefore I assumed you hadn't even tried to do the work since it sounds like a class room question. For you to respond like you did is childish.

Similar Threads

  1. Replies: 5
    Last Post: 01-16-2010, 11:14 PM
  2. Replies: 18
    Last Post: 12-02-2009, 12:01 PM
  3. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM
  4. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04:55 PM
  5. [SOLVED] return statement
    By Nakira in forum New To Java
    Replies: 8
    Last Post: 11-12-2008, 11:00 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
  •