0

I'm trying to implement a toString method, and the output of the toString depends on the boolean variables. Below is my class and main.

public class Cell {

    public int addSpaces;
    boolean isEmpty;
    boolean isChute;
    boolean isLadder;

    public Cell() {
        addSpaces = 10; //I initialized addSpaces to 10 for testing purpose 
    }

    public boolean isChute() { //first boolean method
        if (addSpaces == -10) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isLadder() {//second boolean method
        if (addSpaces == 10) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isEmpty() { //third boolean method
        if (addSpaces == 0) {
            return true;
        } else {
            return false;
        }
    }

    public String toString() {
        String print;
        if (isChute = true) //if isChute is true return true.
        {
            print = "C10";       // toString output = "C10"
        } else if (isLadder = true) // if isLadder is true return true
        {
            print = "L10";          // toString output == "L10"
        } else {
            print = "---"; // else toString print output = "---"
        }
        return print;
    }

    public static void main(String[] arg) {
        Cell s = new Cell();

        System.out.println(s.addSpaces);
        System.out.println(s);
    }
}

Regardless of the input state of toString, I basically get the same output "C10".

Can someone tell me what I did wrong?

I'm new to this website so I appreciate any feedback for future reference. Thank you.

3
  • 1
    you should have == inside if and not = Commented Mar 15, 2013 at 5:18
  • You as using an assignment operator = for comparison hence only first condition is executed all the time. Use == Commented Mar 15, 2013 at 5:19
  • Thank you @rajesh & Sudhanshu: yes i tried using the if(==) instead of if(=), but then the output is "---". By using == inside the if statement it skips both the if and else if statement and went for the else statement. Commented Mar 15, 2013 at 5:25

2 Answers 2

8

You've fallen into one of the languages "gotchas"

This...

if(isChute = true) //if isChute is true return true.
    print = "C10";       // toString output = "C10"
else if (isLadder = true) // if isLadder is true return true
    print = "L10";          // toString output == "L10"
else   
    print = "---"

is actually assigning true to isChute. You should be using == not =

Updated

A better approach would be...

if(isChute) //if isChute is true return true.
    print = "C10";       // toString output = "C10"
else if (isLadder) // if isLadder is true return true
    print = "L10";          // toString output == "L10"
else   
    print = "---"

If there are only two states that the object can be (either a chute or ladder), you could simply use

if(isChute) //if isChute is true return true.
    print = "C10";       // toString output = "C10"
else print = "L10";          // toString output == "L10"

If it can have more then 2 states then I would use an enum type instead.

Sign up to request clarification or add additional context in comments.

4 Comments

my first intention of this method was to check if the booleans are true, and the output of the toString would depends on those cases. So is my method wrong? Thank You.
No, your method's not wrong, but the way you are trying to determine the state is
i'd tried implementing your first method and the output is "---". It seems like the if and else if statement are false. I tried a difference approach to check my boolean methods. System.out.println(s.isLadder); System.out.println(s.isChute); System.out.println(s.isEmpty); and the output is false, false , false. Would this be a reason effecting the if statement? Thank you
Yep, if all the states are false then it will display "---". In your case, I would use enum with three possible states Ladder, Chute and Empty. This way, it can only ever be one of these states...
0

isChute is assigned to true. So "C10" is being returned all the time by toString(). Change it

if(isChute){
    ...
}else if(isLadder){
    ...
}else{
    ..
}

2 Comments

Thank you Nandakisshore, but if i don't put the == or = in the if statement, only the else if would get execute.
You have named the function same as the variable. So if you want the function return value as the expression value, then isChute() has to be used in the expression field of if statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.