DavidGilly
DavidGilly

Reputation: 47

How do I return something as true or false when I have an if() statement?

I don't exactly know how to word this, but I want to know how to return whether something is true or not depending on what the if statement below determines. I'm sorry if you can't understand what I'm asking, but if you look at the code I think you will understand what I'm asking.

Code

public boolean isFinished()
    {
        int row = 0;
        int column = 0;
    if(box[row][column].getValue() != 0)
            {
                if(box[row][column].getValue == box[row][column+1].getValue() && box[row][column].getValue == box[row][column+2].getValue())
                {
                    return true;
                }
                else if(box[row+1][column].getValue == box[row+1][column+1].getValue() && box[row+1][column].getValue == box[row+1][column+2].getValue())
                {
                    return true;
                }
                 else if(box[row+2][column].getValue == box[row+2][column+1].getValue() && box[row+2][column].getValue == box[row+2][column+1].getValue())
                {
                    return true;
                }
                 else if(box[row][column].getValue == box[row+1][column].getValue() && box[row][column].getValue == box[row+2][column].getValue())
                {
                    return true;
                }
                 else if(box[row][column+1].getValue == box[row+1][column+1].getValue() && box[row][column+1].getValue == box[row+2][column+1].getValue())
                {
                    return true;
                }
                 else if(box[row][column+2].getValue == box[row+1][column+2].getValue() && box[row+1][column].getValue == box[row+2][column+2].getValue())
                {
                    return true;
                }
                else
                 {
                     return false;
                 }
            }
            //return whether it's true or false
        }

Upvotes: 0

Views: 231

Answers (3)

user2917778
user2917778

Reputation:

boolean functions always want something at the end just take the else out and leave return false at the very end it will work. If it get to a return true statement it will pop out of the method before it even hits the false statement at the end.

Test it using an if statement,

if(isFinished){
    //returned true
    //blah blah do something.
}

Upvotes: 0

Nassim
Nassim

Reputation: 301

simply test your getValue() method in a final if statement and then if you get the value you are looking for return true.

Upvotes: 0

nanofarad
nanofarad

Reputation: 41281

You are already returning true for box[row][column].getValue() != 0. In any of those branches, once return is hit, the JVM returns from that method, not just the if-statement.

Now just add an else block to that outer if-then block, that returns something meaningful for your logic.

Upvotes: 1

Related Questions