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 7 of 7

Thread: Missing return statement

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Missing return statement

    I will be calling this method in order to test whether a game of Tic-Tac-Toe is a draw or not by transversing a 3x3 character array named "board". If the array contains a Null character ('u/0000') at any point, the game can still continue and will return false, while if all the spaces in the array are occupied by something besides a Null character, the game cannot continue and is a draw, returning true. The problem is that I get a missing return statement on the line containing the last curly brace:

    public static boolean isAdraw()
      {
        for(int i = 0; i < board.length; i++)
        {
          for(int j = 0; j < board.length; i++)
          {
            if(board[i][j] == NUL_CHAR)
            {
              return false;
            }
            else if(board[i][j] != NUL_CHAR)
            {
              return true;
            }
          }
        }
      }   //<----- I get the error on this line right here

    what seems to be the issue? and feel free to ask for the rest of my code if it pertains to the solution, I just wanted to make it easier to read.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Missing return statement

    Ask yourself at what point you know there are no null values anywhere. The method should return true at that point and not before.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Missing return statement

    The compiler expect, if the second if statement [else if(board[i][j] != NUL_CHAR)] also false then this method does not return any boolean value. That's why it showing 'missing return statement' compile time error.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Location
    South Africa
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Missing return statement

    Do not specify the second condition:- if(board[i][j]!!=NUL_CHAR)
    Write something like if<condition> dothis else dothat. Also your logic seems to be incorrect, it looks like only the first value gets checked.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Missing return statement

    Hi ,use the simple if like

    if(board[i][j] == NUL_CHAR)
    {
    return false;
    }

    if(board[i][j] != NUL_CHAR)
    {
    return true;
    }

    or add the return statement at the end of the method.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Missing return statement

    Thanks for all your help guys, I ended up removing the else if statement, and putting the return true; line outside of the loops, but within the method, and it works!

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Missing return statement

    Well done! Yes, you don't know there are no null values at all until after both loops have finished.

Similar Threads

  1. Missing return statement
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 06:11 AM
  2. Missing return statement
    By Stn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2011, 08:03 PM
  3. Missing Return Statement in If-elseIf-else
    By chronoz13 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 12th, 2009, 07:01 AM
  4. 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
  5. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM

Tags for this Thread